← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Meta software engineering interview with a classic array manipulation problem. The follow-up caught me more off guard than the main question, which I didn't expect.

Questions Asked (2)

Q1

Given a sorted non-decreasing array of integers, modify it in-place so each distinct value appears exactly once, preserving order. Return the new length of the valid prefix, and justify that your solution runs in O(n) time and uses O(1) extra space.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Two-pointer approach clicked pretty fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique: one pointer (write) tracks the position for the next unique element, and the other (read) scans the array. Since the array is sorted, duplicates are adjacent, so compare the current element with the last written unique element and copy only when different. Return the write pointer as the new length.

Pro tip: Explicitly state the invariants: the prefix up to write-1 contains all unique elements in order, and read >= write always. This demonstrates rigorous thinking and makes the O(n) time and O(1) space justification trivial.

1. Clarify and Confirm

Restate the problem to ensure understanding: in-place modification, return new length, preserve order, O(n) time and O(1) space. Ask if empty array or single element is possible.

2. Outline the Two-Pointer Approach

Explain that you'll maintain a write index starting at 1 (since first element is always unique) and iterate read from 1 to n-1. When nums[read] != nums[write-1], copy nums[read] to nums[write] and increment write.

3. Walk Through an Example

Trace the algorithm on a small example like [1,1,2,3,3] to show how the array is modified and why the prefix remains correct. Highlight that elements beyond the new length are irrelevant.

4. Analyze Complexity

Justify O(n) time: each element is visited once by the read pointer. Justify O(1) space: only two integer pointers are used, no additional data structures.

5. Discuss Edge Cases and Trade-offs

Mention edge cases: empty array (return 0), all duplicates (return 1), no duplicates (return n). Discuss why in-place is beneficial (memory efficiency) and any potential drawbacks (modifies input).

Key Points to Mention

  • Two-pointer technique with write and read indices
  • Leveraging sorted property: duplicates are adjacent
  • In-place modification without extra data structures
  • Time complexity O(n) because each element is processed once
  • Space complexity O(1) because only constant extra variables are used
  • Edge cases: empty array, single element, all duplicates, no duplicates

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

Q2

Modify your previous solution so that each value may appear at most twice instead of exactly once. Walk through what changes and why.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I got a little shaky.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the previous solution likely removed duplicates entirely (e.g., in a sorted array or linked list). Then explain that the modification requires allowing up to two occurrences of each value, which means adjusting the removal condition to permit one duplicate. Walk through the specific code changes, such as using a counter or a two-pointer technique with a gap of two, and justify why the time and space complexity remain optimal.

Pro tip: Emphasize that the key is to change the condition from 'if current != previous' to 'if current != previous or count < 2', and mention that this pattern generalizes to 'at most k duplicates' by tracking a count or using a pointer that lags by k. This shows you understand the underlying principle, not just the specific case.

1. Restate the problem and previous solution

Briefly summarize the original problem (e.g., remove duplicates from sorted array) and the approach used (e.g., two-pointer technique). This sets the context for the modification.

2. Identify the change in requirement

Explain that instead of allowing each value exactly once, we now allow at most twice. This means we need to permit one duplicate for each value.

3. Adjust the algorithm condition

Modify the condition that decides whether to keep the current element. For example, in the two-pointer approach, compare the current element with the element at write_index - 2 (if it exists) instead of write_index - 1. If they are different, it's safe to write; otherwise, skip.

4. Walk through an example

Trace the modified algorithm on a sample input (e.g., [1,1,1,2,2,3]) to show how it correctly keeps at most two of each value and returns the new length.

5. Analyze complexity and edge cases

State that time complexity remains O(n) and space O(1). Discuss edge cases like arrays with length 0, 1, or 2, and all elements the same.

Key Points to Mention

  • The condition change: from comparing with the previous element to comparing with the element two positions back in the write pointer.
  • Use of a two-pointer technique (read and write pointers) to achieve in-place modification.
  • Generalization to 'at most k duplicates' by comparing with the element k positions back.
  • Time complexity remains O(n) and space O(1).
  • Edge cases: empty array, array with fewer than 3 elements, and arrays with many duplicates.
  • The importance of maintaining the sorted order and not using extra space.

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