Skip to the solution
MediumLow-Level DesignAsked in 20247 min read

Custom Set class with hash

2.1k views

The problem

Problem, as an interviewer would state it: "Implement a Set-like class from scratch using an actual hash table — an array of 'buckets', each holding a list of values that hash to the same slot — supporting add, has, and delete."

Examples:

Code
const set = new HashSet();
set.add("apple").add("banana");
set.has("apple"); // true

Clarifying questions expected:

  • How should hash COLLISIONS (two different values landing in the same bucket) be handled?
  • Does adding an already-present value need to be a genuine no-op, matching real Set semantics?
  • Is a fixed bucket count acceptable, or should the table resize as it grows?

Code / implementation expected: Yes — real, direct proof of correct add/has/delete, PLUS a deliberately forced real collision scenario confirming multiple colliding values remain correctly, independently trackable.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: a HashSet correctly handles normal usage AND a deliberately forced single-bucket collision, tracking every colliding value independently

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 70 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Count Occurrences of Each Element in an Array (Frequency Counter)

Continue