Common Mistakes

Common errors to avoid

  • Checking existence after insertion: In Two Sum, check if complement exists before adding current element to avoid matching element with itself.
  • Using wrong key type: Mutable objects as keys cause bugs.

Use immutable types like strings, tuples, or frozen sets.

  • Not handling hash collisions mentally: While hash maps handle collisions internally, understanding worst-case O(n)O(n) helps in interviews.
  • Forgetting to initialize counters: Use defaultdict or check existence before incrementing to avoid KeyError.