Approach verbose data-structure design
Company: Hudson River Trading
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design and implement complex data structures, testing algorithmic reasoning, time/space complexity analysis, state modeling, and organization of code and tests.
Constraints
- 0 <= capacity <= 100000
- 0 <= len(operations) <= 200000
- Each operation is either [1, key, value] or [2, key]
- -1000000000 <= key, value <= 1000000000
- The intended solution should run each operation in O(1) average time
Examples
Input: (2, [[1, 1, 1], [1, 2, 2], [2, 1], [1, 3, 3], [2, 2], [1, 4, 4], [2, 1], [2, 3], [2, 4]])
Expected Output: [1, -1, -1, 3, 4]
Explanation: GET 1 returns 1 and makes key 1 most recent. Adding key 3 evicts key 2. Adding key 4 later evicts key 1.
Input: (2, [[1, 1, 1], [1, 2, 2], [1, 1, 10], [1, 3, 3], [2, 1], [2, 2], [2, 3]])
Expected Output: [10, -1, 3]
Explanation: Updating key 1 changes its value to 10 and makes it most recent, so key 2 is evicted when key 3 is inserted.
Hints
- Use a hash map to find a key's stored node in O(1) time.
- Use a doubly linked list to maintain least-recently-used to most-recently-used order, and move nodes to the end when they are accessed.