PracHub
QuestionsLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Meta

Design an Expected O(1) Randomized Container

Last updated: Jul 22, 2026

Quick Overview

Design a distinct-element container with expected constant-time insertion and uniformly random removal. The discussion covers mutation invariants, uniformity, empty-state behavior, expected complexity, deterministic randomness tests, and how duplicate or concurrent variants would change the contract.

  • medium
  • Meta
  • Software Engineering Fundamentals
  • Software Engineer

Design an Expected O(1) Randomized Container

Company: Meta

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

# Design an Expected O(1) Randomized Container Design a generic container that stores distinct elements and supports: - `insert(element) -> bool`: add an element if absent and report whether insertion occurred. - `pop_random() -> element`: remove and return one currently stored element, with every stored element having equal probability. Raise a documented empty-container error when no element exists. Both operations should take expected `O(1)` time. Explain the representation, update invariants, randomness requirement, and tests. You may assume stored elements are hashable. ### Constraints & Assumptions - Duplicate elements are not stored. - The random generator can choose a uniformly distributed integer index. - Preserving insertion order is not required. - Thread safety is not required initially, but should be discussed as a follow-up. ### Clarifying Questions to Ask - Are duplicates rejected or counted as separate occurrences? - What exception or return value represents an empty pop? - Is reproducible seeded randomness needed for tests? - Must concurrent callers be supported? ### What a Strong Answer Covers - A dense array plus an element-to-index map - Swap-with-last removal and every required index update - Expected-time and memory analysis - Uniformity, deterministic testing seams, and empty-state behavior - Clear invariants and edge cases such as removing the last element ### Follow-up Questions - How would the design change if duplicates were allowed? - How would you support weighted random removal? - What synchronization would make operations linearizable? - How would you test distribution quality without flaky assertions?

Quick Answer: Design a distinct-element container with expected constant-time insertion and uniformly random removal. The discussion covers mutation invariants, uniformity, empty-state behavior, expected complexity, deterministic randomness tests, and how duplicate or concurrent variants would change the contract.

Related Interview Questions

  • Design a Concurrent, Memory-Bounded Tally Service - Meta (medium)
  • Troubleshoot a production server outage - Meta (medium)
  • Troubleshoot a Midnight Web Server Outage - Meta (medium)
  • Design a Trade Ledger Class - Meta (easy)
|Home/Software Engineering Fundamentals/Meta

Design an Expected O(1) Randomized Container

Meta logo
Meta
Mar 2, 2026, 12:00 AM
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

Design an Expected O(1) Randomized Container

Design a generic container that stores distinct elements and supports:

  • insert(element) -> bool : add an element if absent and report whether insertion occurred.
  • pop_random() -> element : remove and return one currently stored element, with every stored element having equal probability. Raise a documented empty-container error when no element exists.

Both operations should take expected O(1) time. Explain the representation, update invariants, randomness requirement, and tests. You may assume stored elements are hashable.

Constraints & Assumptions

  • Duplicate elements are not stored.
  • The random generator can choose a uniformly distributed integer index.
  • Preserving insertion order is not required.
  • Thread safety is not required initially, but should be discussed as a follow-up.

Clarifying Questions to Ask Guidance

  • Are duplicates rejected or counted as separate occurrences?
  • What exception or return value represents an empty pop?
  • Is reproducible seeded randomness needed for tests?
  • Must concurrent callers be supported?

What a Strong Answer Covers Guidance

  • A dense array plus an element-to-index map
  • Swap-with-last removal and every required index update
  • Expected-time and memory analysis
  • Uniformity, deterministic testing seams, and empty-state behavior
  • Clear invariants and edge cases such as removing the last element

Follow-up Questions Guidance

  • How would the design change if duplicates were allowed?
  • How would you support weighted random removal?
  • What synchronization would make operations linearizable?
  • How would you test distribution quality without flaky assertions?
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Meta•More Software Engineer•Meta Software Engineer•Meta Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.