FastPrepConstant-Time Key Store Built on Arrays
Problems/Constant-Time Key Store Built on Arrays

Constant-Time Key Store Built on Arrays

Patreon logoPatreon● MediumFULLTIMEONSITE INTERVIEW

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported by a candidate who interviewed at Patreon in December 2021 for a Software Engineer role in the United States. After a phone screen, four more interviews were set up: a programming round, a design round, a manager conversation on team fit, and one more on culture fit. In the programming round the task was a small system for storing data and reading it back in which every operation runs in O(1) time. The candidate first worked through a basic approach, which turned out not to be enough, and then proposed a doubly linked list; the interviewer turned the list down and pointed the candidate to arrays instead, which the candidate felt made the solution harder. With hints from the interviewer the candidate wrote a version that appeared to work, then wrote tests, which turned up edge cases; time ran out just before the candidate had handled them all; the interviewer told the candidate that few people spot them. We list the programming round as an onsite round; this is our reading of a set of four interviews set up after the phone screen.

Our choices: the store holds distinct integer keys and starts empty; the four operations insert, remove, indexOf and get, what each one does and what it returns; that keys fill positions 0 to size - 1 with no gaps and that a removal moves the last key into the freed position; that the calls arrive as two lists and that only indexOf and get add to the answer; all limits; and all three examples. The random-key follow-up is ours, a discussion question answered in the editorial and not tested.

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) adds key at position size, after every stored key. If key is already stored, nothing changes.
  • remove(key) takes key out of the store. If key was not in the last position, the key in the last position moves into the position key held. If key is not stored, nothing changes.
  • indexOf(key) returns the position of key, or -1 when key is not stored.
  • get(position) returns the key at position, or -1 when position is at least size.

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 * 104
  • args.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
All Patreon interview questionsSee Patreon hiring insights
Saved
class Solution {
    public int[] runStore(String[] operations, int[] args) {
        // Write your code here.
    }
}
Checking account…