Constant-Time Key Store Built on Arrays
Updated
Problem statement
Build a store for keys in which every operation runs in O(1) time. Build it on arrays rather than a linked list.
Assumption: the keys are distinct integers. The store keeps them in positions 0 through size - 1 with no gaps, where size is the number of keys stored right now. The store has four operations:
insert(key)addskeyat positionsize, after every stored key. Ifkeyis already stored, nothing changes.remove(key)takeskeyout of the store. Ifkeywas not in the last position, the key in the last position moves into the positionkeyheld. Ifkeyis not stored, nothing changes.indexOf(key)returns the position ofkey, or-1whenkeyis not stored.get(position)returns the key atposition, or-1whenpositionis at leastsize.
Assumption: the store starts empty, and you receive the calls as two lists of equal length, in call order. operations[i] names the call, "insert", "remove", "indexOf" or "get", and args[i] is its one number: the key, or for get the position. Process every call and return the answers of the indexOf and get calls, in call order.
Follow-up: how would you add an operation that returns one of the stored keys, chosen uniformly at random, still in O(1) time?
Function
runStore(operations: String[], args: int[]) → int[]Examples
Example 1
operations = ["insert", "insert", "insert", "remove", "indexOf", "get", "get", "indexOf"]args = [5, 9, 2, 5, 2, 0, 2, 5]return = [0, 2, -1, -1]After the three inserts, keys 5, 9 and 2 sit in positions 0, 1 and 2. Removing 5 frees position 0, and 2, the key in the last position, moves there. So indexOf(2) returns 0 and get(0) returns 2. Two keys are left, so get(2) returns -1, and indexOf(5) returns -1 because 5 is gone.
Example 2
operations = ["insert", "insert", "insert", "remove", "get", "remove", "remove", "get", "insert", "indexOf", "insert", "indexOf"]args = [7, 3, 7, 3, 1, 7, 7, 0, 3, 3, 7, 7]return = [-1, -1, 0, 1]The second insert(7) changes nothing, since 7 is already stored. Key 3 is in the last position, so removing it moves no key, and get(1) returns -1. Removing 7 empties the store; the second remove(7) changes nothing, and get(0) returns -1. Keys inserted again go to the end like new keys: 3 lands in position 0 and 7 in position 1.
Example 3
operations = ["insert", "insert", "insert", "insert", "remove", "remove", "indexOf", "indexOf", "get", "insert", "remove", "get", "indexOf"]args = [10, 20, 30, 40, 20, 10, 30, 40, 1, 50, 30, 0, 50]return = [0, 1, 40, 50, 0]Removing 20 moves 40 from position 3 to position 1. Removing 10 then moves 30, now the last key, from position 2 to position 0. So 30 is in position 0 and 40 in position 1. Key 50 goes to position 2, and removing 30 moves 50 into position 0.
Constraints
1 <= operations.length <= 3 * 104args.length == operations.length- Every
operations[i]is"insert","remove","indexOf"or"get"; at least one is"indexOf"or"get". 0 <= args[i] <= 105
More Patreon interview questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Three SumOA · Seen Apr 2025
Similar questions at other companies
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Insert, Delete, And Get Random In Constant TimeAmazon · ONSITE INTERVIEW
- Insert, Delete, And Get Random In Constant TimeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions
- Array With Set AllMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Insert, Delete, and Get Random in Constant TimeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Randomized Collection with Duplicate ValuesOkta · ONSITE INTERVIEWOkta interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions