← Meta Interview Insights

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

SeniorPrefer not to say
Jul 2026

Summary

Meta software engineering interview with a heavy algorithms focus. Six coding problems across a single session, ranging from probability and expression parsing to linked list manipulation. No behavioral stuff from what I remember, just back-to-back coding.

Questions Asked (6)

Q1

Design a class that takes a list of positive integer weights and supports randomly picking an index with probability proportional to each weight. Follow-up: can you do it with O(1) extra space by mutating the input?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The prefix sum approach came to me pretty fast, binary search on top of that for the sampling step.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and constraints, then propose a solution using prefix sums and binary search for O(log n) time per pick with O(n) extra space. For the follow-up, explain how to mutate the input array in-place to achieve O(1) extra space, likely using a linear scan with cumulative weights and a random target.

Pro tip: Discuss the trade-offs between time and space, and mention that mutating the input is acceptable only if the problem allows it; otherwise, a copy is needed. Also, consider edge cases like zero weights or empty lists.

1. Clarify requirements and constraints

Ask about input size, frequency of picks, whether the input can be mutated, and if weights can be zero. This shows attention to detail and helps tailor the solution.

2. Propose initial solution with O(n) space

Describe using prefix sums of weights and binary search to pick an index in O(log n) time. Explain how to generate a random number between 0 and total weight, then find the first prefix sum greater than that number.

3. Address follow-up: O(1) extra space

Explain that by mutating the input array, you can compute cumulative sums in-place and then perform a linear scan to find the index, achieving O(n) time per pick but O(1) extra space. Alternatively, discuss other in-place techniques if applicable.

4. Analyze trade-offs and edge cases

Compare time and space complexities of both approaches. Mention handling of zero weights, empty input, and the impact of mutating the input on the caller.

5. Summarize and conclude

Reiterate the chosen solution based on constraints, and offer to code or discuss further optimizations.

Key Points to Mention

  • Prefix sums and binary search for efficient random selection
  • Time complexity: O(n) preprocessing, O(log n) per pick with O(n) space; O(1) space with O(n) per pick
  • In-place mutation to achieve O(1) extra space
  • Handling edge cases: empty list, zero weights, negative weights (if allowed)
  • Trade-offs between time and space, and when mutation is acceptable
  • Random number generation and uniform distribution

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

Q2

Evaluate an arithmetic expression string with +, -, *, and integer division. No parentheses. Target O(1) extra space.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The O(1) space constraint is the whole point here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the expression is a valid string with no parentheses and integer division truncates toward zero. Then propose a single-pass left-to-right algorithm that maintains a running result and a current term, applying multiplication/division immediately and deferring addition/subtraction. Emphasize O(1) space by using only a few integer variables and no stack.

Pro tip: Explicitly discuss how you handle integer division truncation (e.g., in C++/Java, -3/2 = -1) and mention that you avoid overflow by using appropriate data types or by checking constraints. This shows attention to edge cases and language-specific behavior.

1. Clarify requirements and constraints

Confirm that the expression contains only non-negative integers, operators +, -, *, /, no parentheses, and that division is integer division truncating toward zero. Ask about input size and overflow concerns.

2. Design the single-pass algorithm

Maintain a running total and a current term. Parse numbers and operators left-to-right; for * and /, update the current term immediately; for + and -, add the current term to the total and start a new term with the appropriate sign.

3. Handle integer division and edge cases

Ensure division truncates toward zero as specified. Consider negative numbers, leading/trailing spaces, and potential overflow. Use long long if needed.

4. Analyze complexity and space

State that the algorithm runs in O(n) time and O(1) extra space, as it uses only a few variables. Contrast with a stack-based approach that would use O(n) space.

5. Test with examples

Walk through a few examples like '3+2*2' and ' 3/2 ' to verify correctness, especially operator precedence and division behavior.

Key Points to Mention

  • Operator precedence: multiplication and division have higher precedence than addition and subtraction.
  • Single-pass left-to-right evaluation with a running total and current term.
  • Integer division truncates toward zero; handle negative numbers accordingly.
  • O(1) extra space achieved by avoiding a stack; only a few variables are used.
  • Time complexity is O(n) where n is the length of the string.
  • Edge cases: empty string, single number, leading/trailing spaces, and overflow.

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

Q3

Given a sorted array and a target value, return the first and last index where the target appears. Return [-1, -1] if it's not there. O(log n) expected.

Algorithms & Data Structures
Author's notes

Straightforward binary search variation.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use binary search twice: once to find the leftmost occurrence (by continuing search on the left half when target is found) and once to find the rightmost occurrence (by continuing on the right half). This achieves O(log n) time and O(1) space. Alternatively, a single binary search can find any occurrence, then expand linearly, but that risks O(n) worst-case.

Pro tip: Clarify edge cases upfront (empty array, target absent, all elements equal) and mention that you'd use two separate binary searches to guarantee O(log n) even when the array has many duplicates. Also, discuss how to avoid infinite loops by carefully updating boundaries.

1. Clarify requirements and edge cases

Confirm the array is sorted, may contain duplicates, and that O(log n) is required. Ask about empty input, target not present, and whether the array can be modified.

2. Design two binary searches

Outline a helper function for binary search that finds the first or last occurrence by adjusting the search space based on whether we want leftmost or rightmost.

3. Implement leftmost search

Perform binary search; when target is found, record the index and continue searching in the left half to find an earlier occurrence.

4. Implement rightmost search

Similarly, when target is found, record the index and continue searching in the right half to find a later occurrence.

5. Handle absence and return result

If either search fails to find the target, return [-1, -1]. Otherwise, return the two indices.

Key Points to Mention

  • Time complexity: O(log n) for each binary search, so overall O(log n).
  • Space complexity: O(1) iterative implementation.
  • Handling duplicates: modifying the binary search condition to continue searching after finding a match.
  • Edge cases: empty array, single element, target smaller than all elements, target larger than all elements.
  • Avoiding integer overflow in mid calculation (use low + (high - low) / 2).
  • Alternative approach: single binary search then linear scan, but note it's O(n) worst-case and not optimal.

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

Q4

Find the length of the longest valid parentheses substring in a string of '(' and ')'.

Algorithms & Data Structures
Author's notes

I went for the DP approach first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose an O(n) solution using a stack or dynamic programming. Explain the algorithm step-by-step, analyze time and space complexity, and optionally discuss alternative approaches like two-pass scanning.

Pro tip: Mention that you can solve it in O(1) space with two passes, but the stack approach is simpler to implement and explain. This shows you understand trade-offs and can adapt to constraints.

1. Clarify the problem

Confirm that the substring must be contiguous and well-formed, and discuss edge cases like empty string or no valid substring.

2. Choose an approach

Select a method such as stack-based, dynamic programming, or two-pass scanning. Briefly justify your choice based on simplicity and efficiency.

3. Walk through the algorithm

Explain the chosen algorithm in detail, using a small example to illustrate how it works step by step.

4. Analyze complexity

State the time and space complexity of your solution, and compare with alternatives if relevant.

5. Handle edge cases and test

Discuss how your solution handles edge cases and mentally test with examples like '(()' or ')()())'.

Key Points to Mention

  • Stack-based approach: push indices of '(' and use a base index to compute lengths.
  • Dynamic programming: dp[i] represents the length of the longest valid substring ending at i.
  • Two-pass scanning: left-to-right and right-to-left to handle both '(' and ')' imbalances.
  • Time complexity O(n) and space complexity O(n) for stack/DP, O(1) for two-pass.
  • Edge cases: empty string, no valid parentheses, all '(' or all ')'.
  • Comparison of approaches: stack is intuitive, DP is elegant, two-pass is space-optimal.

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

Q5

Partition a singly linked list around a value x so all nodes less than x come before nodes greater than or equal to x, maintaining relative order within each group.

Algorithms & Data Structures
Author's notes

Two dummy-head pointer approach.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and constraints, then propose a two-pointer approach using two dummy lists to partition nodes while preserving relative order. Walk through an example, analyze time and space complexity, and discuss edge cases and potential optimizations.

Pro tip: Emphasize that this is a stable partition, and mention that using dummy nodes simplifies edge cases. Also, note that the solution can be done in-place with O(1) extra space by rearranging pointers.

1. Clarify requirements and constraints

Ask if the partition should be stable (maintain relative order) and if we can modify the list in-place. Confirm that all nodes less than x come before nodes >= x.

2. Propose approach

Suggest using two dummy nodes to build two separate lists: one for nodes < x and one for nodes >= x. Then concatenate them.

3. Walk through example

Choose a sample list (e.g., 3->5->8->5->10->2->1 with x=5) and show step-by-step how nodes are distributed and linked.

4. Analyze complexity

State that time complexity is O(n) since we traverse the list once, and space complexity is O(1) extra space (excluding the output list) because we only use pointers.

5. Discuss edge cases and optimizations

Mention edge cases: empty list, all nodes < x, all nodes >= x, x not present. Also note that the solution is stable and can be done in-place.

Key Points to Mention

  • Stability: relative order within each partition must be preserved.
  • Use of dummy nodes to simplify edge cases and avoid null pointer errors.
  • In-place rearrangement with O(1) extra space.
  • Time complexity O(n) and space complexity O(1).
  • Handling edge cases: empty list, all nodes less than x, all nodes greater than or equal to x.
  • Potential follow-up: what if the list is doubly linked? Or if we need to partition around multiple values?

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

Q6

Count the total number of palindromic substrings in a string.

Algorithms & Data Structures
Author's notes

Expand-around-center is my go-to for palindrome problems.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: count all palindromic substrings, including duplicates at different positions. Then present an efficient solution, such as expanding around each center (O(n^2) time, O(1) space), and discuss trade-offs with other approaches like DP or Manacher's algorithm.

Pro tip: Mention that you would first confirm whether the count includes single-character palindromes and overlapping substrings, as this affects the implementation. Also, briefly note that Manacher's algorithm can solve it in O(n) if optimal performance is required, showing depth beyond the typical O(n^2) solution.

1. Clarify the problem

Ask if single characters count and if substrings are counted by occurrence (including duplicates). Confirm input constraints (e.g., length, character set) to guide algorithm choice.

2. Discuss brute force and its complexity

Mention that checking all O(n^2) substrings and verifying each palindrome takes O(n^3) time, which is inefficient. This sets the stage for optimization.

3. Present the expand-around-center approach

Explain that every palindrome has a center (a character or between two characters). Expand around each of the 2n-1 centers to count all palindromes in O(n^2) time and O(1) space.

4. Optionally mention advanced approaches

If asked for optimal time, describe Manacher's algorithm which finds all palindromic substrings in O(n) time, though it's more complex to implement.

5. Analyze complexity and edge cases

State time and space complexity clearly. Discuss edge cases: empty string, single character, all same characters, and strings with no palindromes longer than 1.

Key Points to Mention

  • Definition of palindromic substring and that single characters are palindromes
  • Time and space complexity of each approach (brute force, expand around center, DP, Manacher's)
  • Handling of even-length and odd-length palindromes via centers
  • Edge cases: empty string, single character, all identical characters
  • Trade-offs between simplicity (expand around center) and optimality (Manacher's)
  • Potential for using dynamic programming with O(n^2) space, but noting it's less efficient than expand-around-center

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