← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Meta SWE coding round with two LeetCode-style problems. Nothing too exotic but the combination felt deliberate.

Questions Asked (2)

Q1

Given an array of weights, implement a function that randomly picks an index such that the probability of picking each index is proportional to its weight.

Algorithms & Data Structures
Author's notes

Prefix sums plus binary search.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints and edge cases, then propose a solution using prefix sums and binary search to achieve O(n) preprocessing and O(log n) query time. Explain the algorithm step-by-step, analyze its complexity, and discuss potential optimizations or alternative approaches like the alias method.

Pro tip: Mention that the prefix sum array should be built once and reused for multiple random picks, and highlight that using binary search on the prefix sums ensures efficient sampling even for large arrays. Also, briefly note that the alias method can achieve O(1) query time with O(n) preprocessing if queries are frequent.

1. Clarify requirements and edge cases

Ask about input size, number of queries, whether weights can be zero or negative, and if the array can be modified. Confirm that the function should return an index with probability proportional to its weight.

2. Propose prefix sum + binary search approach

Explain that you will compute a prefix sum array where prefix[i] = sum of weights from 0 to i. Then generate a random number between 0 and total sum, and use binary search to find the smallest index where prefix sum exceeds the random number.

3. Walk through an example

Use a small example like weights = [1, 3, 2] to illustrate how the prefix sums are [1, 4, 6], and how a random number in [0,6) maps to indices with correct probabilities.

4. Analyze complexity and discuss optimizations

State that preprocessing takes O(n) time and O(n) space, and each query takes O(log n) time. Mention that if many queries are expected, the alias method can achieve O(1) query time with O(n) preprocessing.

5. Handle edge cases and conclude

Address cases like all weights zero (return any index or throw error), single element, and floating-point weights. Summarize the solution and its trade-offs.

Key Points to Mention

  • Prefix sum array construction and its role in mapping random numbers to indices
  • Binary search to find the target index efficiently
  • Time and space complexity: O(n) preprocessing, O(log n) per query, O(n) space
  • Handling edge cases: zero weights, negative weights (if allowed), empty array
  • Alternative approach: Alias method for O(1) query time
  • Random number generation: using uniform distribution over [0, total sum)

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

Q2

Given an integer array, return a new array where each element is the product of all other elements in the original array, without using division.

Algorithms & Data Structures
Author's notes

Classic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., array size, element types, handling zeros) and then propose an O(n) time and O(n) space solution using prefix and suffix products. Explain that you will compute prefix products in one pass and suffix products in another, then multiply them to get the result without division.

Pro tip: Mention the trade-off between extra space and time: you can achieve O(1) extra space (excluding output) by using the output array to store prefix products first, then a running suffix product variable. This shows you optimize beyond the basic solution.

1. Clarify requirements and edge cases

Ask about input size, possible zeros, negative numbers, and whether the output can be in-place. Confirm that division is not allowed and discuss how zeros affect the result.

2. Outline the prefix-suffix approach

Explain that for each index i, the result is the product of all elements before i (prefix) times the product of all elements after i (suffix). This avoids division and handles zeros naturally.

3. Detail the algorithm

Describe two passes: first, compute prefix products and store in the output array; second, traverse from the right while maintaining a running suffix product, multiplying it into the output array.

4. Analyze complexity and optimize space

State that time complexity is O(n) and space complexity is O(n) for the output array, but extra space can be O(1) if we reuse the output array for prefixes. Mention that this is optimal.

5. Test with examples and edge cases

Walk through a small example (e.g., [1,2,3,4]) and an edge case with zeros (e.g., [0,1,2]) to verify correctness. Discuss how the algorithm handles multiple zeros.

Key Points to Mention

  • Time complexity O(n) and space complexity O(n) for output, with O(1) extra space optimization.
  • Handling of zeros: if there are two or more zeros, all outputs are zero; if one zero, only the position of zero gets the product of non-zero elements.
  • The prefix-suffix technique avoids division and is a common pattern for product-of-array-except-self problems.
  • In-place computation using the output array to store prefix products, then a running suffix product variable.
  • Edge cases: empty array, single element, negative numbers, and integer overflow considerations.
  • Clarify with the interviewer whether the output array counts towards space complexity (usually not).

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