Randomized String Multiset
Updated
Problem statement
Keep a collection of strings that may contain more than one copy of a word. Process the equally sized arrays actions, words and drawIndices in order. Return one string per operation.
INSERTadds one occurrence ofwords[i]. Return"true"if that word was absent immediately before insertion, otherwise"false".REMOVEremoves one occurrence ofwords[i]if present and returns"true"; otherwise return"false".GET_RANDOMreturns the occurrence selected by the supplied draw index. It is called only when the multiset is nonempty.
Reproducible random draws
For this exercise, assume the stored copies occupy an ordered sequence with no empty slots. The sequence starts empty. Insertions append. To remove a word, choose its most recently inserted copy that is still present. Move the sequence’s last entry into that copy’s slot, then remove the last slot. Failed removals do nothing. Each inserted copy keeps its own identity, even when another copy has the same word.
For GET_RANDOM, drawIndices[i] is a supplied zero-based index into that current sequence; return the word at that slot. The input index represents an external uniform random draw. If each draw is independent and uniform on the current slot range, a word with c occurrences among s total occurrences is returned with probability c / s. The supplied test draws need not look random; do not generate new draws, deduplicate the sequence, or sort its slots.
For this exercise, assume words[i] = "" for GET_RANDOM and drawIndices[i] = 0 for the other actions. Return insertion and removal results, as well as selected words, as strings. Aim for expected constant time per operation, excluding the cost of processing a string and amortized dynamic-array resizing.
Function
runStringMultiset(actions: String[], words: String[], drawIndices: int[]) → String[]Examples
Example 1
actions = ["INSERT","INSERT","INSERT","GET_RANDOM","GET_RANDOM","GET_RANDOM"]words = ["cat","cat","dog","","",""]drawIndices = [0,0,0,0,1,2]return = ["true","false","true","cat","cat","dog"]The dense slots are cat,cat,dog: two of three uniform indices select cat.
Example 2
actions = ["INSERT","INSERT","INSERT","INSERT","REMOVE","GET_RANDOM","REMOVE","GET_RANDOM"]words = ["red","blue","red","green","red","","red",""]drawIndices = [0,0,0,0,0,2,0,0]return = ["true","true","false","true","true","green","true","green"]The newest red is in slot 2, so green fills it; removing the remaining red then moves green to slot 0.
Example 3
actions = ["REMOVE","INSERT","REMOVE","INSERT","GET_RANDOM"]words = ["x","x","x","x",""]drawIndices = [0,0,0,0,0]return = ["false","true","true","true","x"]A missing removal is false. Once the last occurrence is removed, a later insertion is new again.
Constraints
- The three input arrays have the same length
n, where0 <= n <= 1000. - Each action is
INSERT,REMOVE, orGET_RANDOM. - Inserted and removed words contain between
1and30lowercase English letters. - Every random operation has
0 <= drawIndices[i] < current occurrence count; its word field is empty. - Non-random operations have draw index
0.
More LinkedIn Phone Screen questions
- Maximum Product of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Maximum Sum of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
- Merge Two Sorted ListsPHONE SCREEN · Seen Nov 2026
- Sqrt(x)PHONE SCREEN · Seen Nov 2026
- Total Years of Work ExperiencePHONE SCREEN · Seen Nov 2026
- Sum an Arbitrary-Dimensional ArrayPHONE SCREEN · Seen Oct 2026
- Known Words Matching Phone DigitsPHONE SCREEN · Seen Oct 2026
- Inverse-Depth Nested List SumPHONE SCREEN · Seen Oct 2026
- Valid Number with ExponentsPHONE SCREEN · Seen Oct 2026
- Sort a Quadratic-Transformed ArrayPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Chatter Message WindowReddit · PHONE SCREENReddit interview questions
- Versioned Recipe Shopping CartDecagon · PHONE SCREENDecagon interview questions
- Insert Delete GetRandom O(1) - Duplicates AllowedBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Logger Rate LimiterGoogle · PHONE SCREENGoogle interview questions
- Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
- Portfolio Tickers with Constant-Time Random PickCitadel · PHONE SCREENCitadel interview questions
- Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
- Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Linear-Probing Key-Value StoreEase · PHONE SCREEN