Small trading company | Hashmap with automatic deletion
Anonymous User
1020

It's an on-site technical interview for an internship position in a small trading company. The question was given orally.
We want to save key and value pairs into a hashmap. The problem is that we have limited space, so we should delete pairs that are not accessed in a certain amount of time. Each time, we can either add/update key, value pair or try to get the value of a certain key.
Here's an example, suppose that entries should be deleted after 3 unit of time

ka va 1 // add key ka, value va, at time 1
kb vb 1 // add key kb, value vb, at time 1
vb 2 // try to access vb at time 2

In the example, at time 4 entry ka,va should be deleted while kb,vb should still remain.

Here was my solution:

  1. Has a queue of commands, and another hashmap for the key and time.
  2. Check whether the first command in the queue's time + time for deletion is more than the current command time.
  3. If yes, check the time hashmap if it's the same with the one in the queue's time, delete the pair from the original hashmap.
  4. Go back to 2.

I was rejected the second the interview ended, so my solution was really bad lol.

Comments (2)