← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

LinkedIn SWE interview with a data structure design problem that looked clean on paper but had some tricky edge cases around duplicates. Not a bad experience, just required more careful thinking than I expected.

Questions Asked (1)

Q1

Design a data structure that supports add, delete, and getRandom operations all in average O(1) time, where duplicates are allowed and getRandom should return elements with probability proportional to their frequency.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

The random part is what gets you.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that duplicates are allowed and getRandom must return elements with probability proportional to frequency. Propose a hybrid data structure combining a dynamic array of elements (with duplicates) and a hash map from element to a set of indices, enabling O(1) add, delete, and getRandom by swapping with the last element. Explain how to handle duplicates by storing multiple indices per element and updating the map during swaps.

Pro tip: Mention that using a set of indices per element allows O(1) deletion even with duplicates, but if the interviewer prefers a simpler approach, you can use a list of indices and swap with the last index in that list. Also, discuss the trade-off between memory overhead and simplicity.

1. Clarify requirements and constraints

Confirm that duplicates are allowed, getRandom should be weighted by frequency, and all operations must be average O(1). Ask about memory constraints and whether elements can be any type.

2. Design core data structures

Use a dynamic array to store all elements (including duplicates) and a hash map from element to a set (or list) of indices where it appears. This allows O(1) access and updates.

3. Implement add operation

Append the element to the array, record its index in the hash map, and increment its frequency count (if needed).

4. Implement delete operation

To delete one occurrence of an element, retrieve an index from its set, swap that element with the last element in the array, update the index sets for both elements, and remove the last element.

5. Implement getRandom operation

Generate a random index uniformly from 0 to array length-1 and return the element at that index. Since duplicates are stored multiple times, the probability is proportional to frequency.

Key Points to Mention

  • Use of a dynamic array to store elements with duplicates, enabling O(1) random access by index.
  • Hash map mapping each unique element to a collection (set or list) of its indices in the array.
  • Swap-with-last technique for O(1) deletion, ensuring the array remains compact.
  • Handling duplicates by allowing multiple indices per element and updating the map during swaps.
  • Time complexity analysis: average O(1) for add, delete, and getRandom due to hash map operations and array swaps.
  • Space complexity: O(n) where n is the total number of elements, with overhead for the hash map and index sets.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.