educative.io

Educative

Incorrect code shown in What is a Hash Table?

In the lesson “What is a Hash Table?” in the DS for coding interview in Java, this is the given code -

class Hashing {
   public static String getValue(int key)  
   {    
    String [] myString = // pun intended
    { 
      "I am a programmer, I have no life.",
      "8 bytes walk into a bar, the bartenders asks \"What will it be?\"One of them says, \"Make us a double.\"",
      "Computers are useless. They can only give you answers.\n-Pablo Picasso"
    }; 
    if (key <= myString.length)
      return myString[key];    // value returned in constant time
    else 
      System.out.println("Key Not Found!\n");  
     return "";
  }
    public static void main( String args[] ) 
    {
        System.out.println( getValue(3) ); // Test your output for other keys
    }
}

This line is incorrect - if (key <= myString.length). If you call getValue(3), it will throw an Array Index Out of Bounds exception.

I’m quite disappointed with the quality of these lessons. As I cover more of them, I keep finding more such bugs all throughout. You would think a website that charges so much for its content would be bug free.

1 Like

Hi @Manish_Giri,

I am incredibly sorry that you have to see that, and thanks for your kind input. We have updated our course and removed this Exception. Thank you so much for reaching out to us.