PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Google

Design set with O(1) random access

Last updated: May 21, 2026

Quick Overview

This question evaluates skill in designing a composite data structure that supports average O(1) insertion, deletion, and uniform random access, and tests understanding of hashing, indexing, randomness, and concurrency control.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Design set with O(1) random access

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design a data structure ("FancySet") that stores unique integers and supports the following operations, each in average O(1) time: - `add(x)`: Insert value `x` if it is not already present. Return whether the insert happened. - `delete(x)`: Remove value `x` if it exists. Return whether a deletion happened. - `getRandom()`: Return a uniformly random element currently in the set. Requirements / follow-ups: - All operations should be average O(1). - Handle corner cases such as calling `getRandom()` on an empty set (define and justify behavior). - Discuss how you would make this data structure thread-safe. Compare using a single lock for the whole set vs finer-grained locking (e.g., element-level locks), and explain trade-offs.

Quick Answer: This question evaluates skill in designing a composite data structure that supports average O(1) insertion, deletion, and uniform random access, and tests understanding of hashing, indexing, randomness, and concurrency control.

Related Interview Questions

  • Solve Flower Placement and Directory Deletion - Google (medium)
  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Determine Whether a Word Exists in a Graph - Google (medium)
  • Implement Checksums and Feature Rollout Evaluation - Google (medium)
Google logo
Google
Jan 1, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
7
0

Design a data structure ("FancySet") that stores unique integers and supports the following operations, each in average O(1) time:

  • add(x) : Insert value x if it is not already present. Return whether the insert happened.
  • delete(x) : Remove value x if it exists. Return whether a deletion happened.
  • getRandom() : Return a uniformly random element currently in the set.

Requirements / follow-ups:

  • All operations should be average O(1).
  • Handle corner cases such as calling getRandom() on an empty set (define and justify behavior).
  • Discuss how you would make this data structure thread-safe. Compare using a single lock for the whole set vs finer-grained locking (e.g., element-level locks), and explain trade-offs.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.