← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Meta SWE coding round with a probability/random sampling problem. The prefix sum approach is pretty standard but the boundary conditions tripped me up more than I expected.

Questions Asked (1)

Q1

Design a weighted random index picker: given an array of weights, implement an init method and a pickIndex method that returns an index with probability proportional to its weight, with pickIndex running in O(log n) time.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The core idea clicked fast enough, build a prefix sum array and binary search on a random number in the total range.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem requirements, then propose using prefix sums of weights and binary search to achieve O(log n) pickIndex. Explain the init method that precomputes prefix sums in O(n) time, and detail how pickIndex generates a random number and uses binary search to find the index.

Pro tip: Mention edge cases like zero weights and discuss potential optimizations such as using a Fenwick tree for dynamic updates, showing depth beyond the basic solution.

1. Clarify Requirements

Confirm that weights are non-negative, at least one weight is positive, and that pickIndex should run in O(log n) time. Discuss whether weights can be updated after initialization.

2. Design Data Structure

Propose storing prefix sums of weights in an array during init. Explain that this allows mapping a random number to an index via binary search.

3. Implement init

Compute prefix sums in O(n) time, handling zero weights appropriately. Store the total sum for random number generation.

4. Implement pickIndex

Generate a random integer between 1 and total sum (inclusive), then use binary search (e.g., bisect_left) on the prefix sums to find the smallest index where prefix sum >= random number. Return that index.

5. Analyze Complexity and Edge Cases

State that init is O(n) and pickIndex is O(log n) time, O(n) space. Discuss edge cases like all weights zero (invalid), single weight, and large weights causing overflow (use 64-bit integers).

Key Points to Mention

  • Prefix sums to represent cumulative weights
  • Binary search for O(log n) pickIndex
  • Random number generation uniform over [1, total sum]
  • Handling zero weights and ensuring at least one positive weight
  • Time and space complexity analysis
  • Potential follow-up: dynamic updates using Fenwick tree

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