← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta SWE coding round, got a classic shuffling problem. Nothing too surprising but the O(1) space constraint is the part that trips people up if they haven't seen it before.

Questions Asked (1)

Q1

Given n items labeled 0 through n-1, generate a uniformly random permutation in O(n) time using a standard random integer function. Extra space should be minimized.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew Fisher-Yates but blanked for a second on why you swap in-place instead of building a separate structure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints and the random integer function's behavior. Then, describe the Fisher-Yates shuffle algorithm, emphasizing its O(n) time and O(1) extra space (in-place) properties. Walk through the algorithm step-by-step, proving uniformity and analyzing complexity.

Pro tip: Mention that the algorithm is in-place and that the random integer function must generate unbiased random numbers in the correct range; this shows attention to detail and practical implementation concerns.

1. Clarify requirements

Confirm that the permutation must be uniformly random, O(n) time, and minimize extra space. Ask about the random integer function's signature and whether it can generate numbers in a given range.

2. Choose the algorithm

Select the Fisher-Yates shuffle (also known as Knuth shuffle) because it guarantees uniform random permutations in O(n) time and O(1) extra space when implemented in-place.

3. Explain the algorithm

Describe iterating from the last index down to 1, and for each index i, swapping the element at i with the element at a randomly chosen index j where 0 ≤ j ≤ i. Emphasize that each swap uses the random integer function to pick j uniformly.

4. Prove uniformity and complexity

Argue that each permutation is equally likely by showing that the probability of any specific permutation is 1/n!. State that the loop runs n-1 times, each iteration doing O(1) work, resulting in O(n) time and O(1) extra space.

5. Discuss edge cases and implementation

Mention handling of n=0 or n=1, and note that the random integer function should be unbiased. If needed, discuss how to adapt if the function only generates numbers in a fixed range (e.g., using modulo with rejection sampling).

Key Points to Mention

  • Fisher-Yates shuffle (Knuth shuffle) algorithm
  • In-place swapping to achieve O(1) extra space
  • Uniformity proof: each permutation has probability 1/n!
  • Time complexity: O(n) due to n-1 iterations with constant-time operations
  • Correct use of random integer function to pick index in [0, i]
  • Edge cases: n=0, n=1, and unbiased random number generation

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