title: Why can't I pass the hidden hashtable test cases?
body: <p><strong>Coalesce Hashing in C</strong></p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define TABLESIZE 37
  #define PRIME     13
  
  enum Marker {EMPTY,USED};
  
  typedef struct _slot{
     int key;
     enum Marker indicator;
     int next;
  } HashSlot;
  </code></pre>
  <pre><code>int HashInsert(int key, HashSlot hashTable[]);
  int HashFind(int key, HashSlot hashTable[]);
  </code></pre>
  <pre><code>int hash(int key)
  {
     return (key % TABLESIZE);
  }
  </code></pre>
  <pre><code>int main()
  {
      int opt;
      int i;
      int key;
      int index;
      HashSlot hashTable[TABLESIZE];
  
      for(i=0;i&lt;TABLESIZE;i++){
          hashTable[i].next = -1;
          hashTable[i].key = 0;
          hashTable[i].indicator = EMPTY;
      }
  
      printf(&quot;============= Hash Table ============\n&quot;);
      printf(&quot;|1. Insert a key to the hash table  |\n&quot;);
      printf(&quot;|2. Search a key in the hash table  |\n&quot;);
      printf(&quot;|3. Print the hash table            |\n&quot;);
      printf(&quot;|4. Quit                            |\n&quot;);
      printf(&quot;=====================================\n&quot;);
  
      printf(&quot;Enter selection: &quot;);
      scanf(&quot;%d&quot;,&amp;opt);
      while(opt&gt;=1 &amp;&amp; opt &lt;=3){
          switch(opt){
          case 1:
              printf(&quot;Enter a key to be inserted:\n&quot;);
              scanf(&quot;%d&quot;,&amp;key);
              index = HashInsert(key,hashTable);
              if(index &lt;0)
                  printf(&quot;Duplicate key\n&quot;);
              else if(index &lt; TABLESIZE)
                  printf(&quot;Insert %d at index %d\n&quot;,key, index);
              else
                  printf(&quot;Table is full.\n&quot;);
              break;
          case 2:
              printf(&quot;Enter a key for searching in the HashTable:\n&quot;);
              scanf(&quot;%d&quot;,&amp;key);
              index = HashFind(key, hashTable);
  
              if(index!=-1)
                  printf(&quot;%d is found at index %d.\n&quot;,key,index);
              else
                  printf(&quot;%d is not found.\n&quot;,key);
              break;
  
          case 3:
              printf(&quot;index:\t key \t next\n&quot;);
              for(i=0;i&lt;TABLESIZE;i++) printf(&quot;%d\t%d\t%d\n&quot;,i, hashTable[i].key,hashTable[i].next);
              break;
          }
          printf(&quot;Enter selection: &quot;);
          scanf(&quot;%d&quot;,&amp;opt);
      }
      return 0;
  }
  </code></pre>
  <pre><code>int HashInsert(int key, HashSlot hashTable[])
  {
      int index=hash(key),inIndex=0,count=0;
  
      if(hashTable[index].indicator == EMPTY) // first slot empty
      {
          hashTable[index].key = key;
          hashTable[index].next = -1;
          hashTable[index].indicator = USED;   
          return index;       
      }
      else // slot used
      {
          while(1)
          {   
              while(hashTable[count].indicator == USED) // all slots used
              {
                  count++;
                  if(count == TABLESIZE)
                  {
                      return count; // return table is full
                  }
              }
  
              if(index&lt;TABLESIZE) // index slot &lt; TABLESIZE
              {
                  for(index=hash(key);index&lt;TABLESIZE;index++) // iterate thru 
                  {
                      if(hashTable[index].key == key)
                      {
                          return -1;
                      }
                      
                      if (hashTable[index].next == -1 &amp;&amp; hashTable[index].indicator == USED &amp;&amp; 
                      hash(key)==hash(hashTable[index].key) )//save the last point where it has the same mod value
                      {
                          inIndex = index;
                      }
  
                      else if(hashTable[index].indicator == EMPTY)
                      {
                          hashTable[inIndex].next = index; // lastpoint.next = currentindex
                          hashTable[index].key = key;
                          hashTable[index].next = -1;
                          hashTable[index].indicator = USED; 
                          return index; // return insertion value
                      }  
                  }
              }
              else
              {
                  for(index=hash(index);index&lt;TABLESIZE;index++) // if index &gt; TABLESIZE
                  {
                      if(hashTable[index].key == key)
                      {
                          return -1;
                      }
  
                      if (hashTable[index].next == -1 &amp;&amp; hashTable[index].indicator == USED &amp;&amp; 
                      hash(key)==hash(hashTable[index].key) ) //save the last point where it has the same mod value
                      {
                          inIndex = index;
                      }
                      else if(hashTable[index].indicator == EMPTY)
                      {
                          hashTable[inIndex].next = index; // latestmodvalue.next = currentindex
                          hashTable[index].key = key;
                          hashTable[index].next = -1;
                          hashTable[index].indicator = USED; 
                          return index; // return insertion value
                      }  
                  }
              }
          }
  
      }
  
      if(HashFind(key,hashTable) == index) // if key cant be found
          return -1;
  }
  </code></pre>
  <pre><code>int HashFind(int key, HashSlot hashTable[])
  {
      int index;
      index = hash(key);
  
      if(hashTable[index].key == 0) return -1; // if empty slot , return -1
      else if(hashTable[index].key == key) // if found key , return index
      {
          return index;
      }
      else
      {
          while(hashTable[index].key!=key &amp;&amp; index!=-1) // find the link , then find the key
          {
              index = hashTable[index].next;
          }
          return index;
      }
  }
  </code></pre>
  <p>The idea is to implement a hash table with a combination of closed addressing and linear probing. I have tried multiple test cases that I came up with such as dealing with negatives or accessing an item that is not in the HashTable, nothing seems to pass the hidden test case. Passed the sample test case though. Is there something that I am overlooking on?</p>
  <p>Given test case :</p>
  <p>Insert key : 1 2 5 41 42  4 10(search then insert)</p>
  <p>Search key: 4 10(Display DNE)</p>
  <h2>Output from my Code</h2>
  <pre><code>============= Hash Table ============
  |1. Insert a key to the hash table  |
  |2. Search a key in the hash table  |
  |3. Print the hash table            |
  |4. Quit                            |
  =====================================
  Enter selection: 1 Enter a key to be inserted:
  Insert 1 at index 1
  Enter selection: 1 Enter a key to be inserted:
  Insert 2 at index 2
  Enter selection: 1 Enter a key to be inserted:
  Insert 5 at index 5
  Enter selection: 1 Enter a key to be inserted:
  Insert 41 at index 4
  Enter selection: 1 Enter a key to be inserted:
  Insert 42 at index 6
  Enter selection: 3
  index:   key:    next:
  0   0   -1
  1   1   -1
  2   2   -1
  3   0   -1
  4   41  -1
  5   5   6
  6   42  -1
  7   0   -1
  8   0   -1
  9   0   -1
  10  0   -1
  11  0   -1
  12  0   -1
  13  0   -1
  14  0   -1
  15  0   -1
  16  0   -1
  17  0   -1
  18  0   -1
  19  0   -1
  20  0   -1
  21  0   -1
  22  0   -1
  23  0   -1
  24  0   -1
  25  0   -1
  26  0   -1
  27  0   -1
  28  0   -1
  29  0   -1
  30  0   -1
  31  0   -1
  32  0   -1
  33  0   -1
  34  0   -1
  35  0   -1
  36  0   -1
  Enter selection: 1 Enter a key to be inserted:
  Insert 4 at index 7
  Enter selection: 2 Enter a key for searching in the HashTable:
  4 is found at index 7.
  Enter selection: 2 Enter a key for searching in the HashTable:
  10 is not found.
  Enter selection: 1 Enter a key to be inserted:
  Insert 10 at index 10
  Enter selection: 3
  index:   key:   next:
  0   0   -1
  1   1   -1
  2   2   -1
  3   0   -1
  4   41  7
  5   5   6
  6   42  -1
  7   4   -1
  8   0   -1
  9   0   -1
  10  10  -1
  11  0   -1
  12  0   -1
  13  0   -1
  14  0   -1
  15  0   -1
  16  0   -1
  17  0   -1
  18  0   -1
  19  0   -1
  20  0   -1
  21  0   -1
  22  0   -1
  23  0   -1
  24  0   -1
  25  0   -1
  26  0   -1
  27  0   -1
  28  0   -1
  29  0   -1
  30  0   -1
  31  0   -1
  32  0   -1
  33  0   -1
  34  0   -1
  35  0   -1
  36  0   -1 
  </code></pre>