← Bytedance Interview Insights

Bytedance·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Bytedance SWE interview with an in-place array problem. Pretty standard algorithmic round but the two-pointer constraint made it trickier than it looked at first glance.

Questions Asked (1)

Q1

Given a sorted integer array, remove duplicates in-place so each unique element appears at most twice. Return the count of remaining elements. Must be O(n) time and O(1) space.

Algorithms & Data Structures
Author's notes

The two-pointer setup clicked pretty fast for me.

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 where the next valid element should be placed, and the other (read) scans the array. Since each element can appear at most twice, compare the current element with the element at write-2; if different, copy it to write and increment write. Return write as the new length.

Pro tip: Clarify that the array is sorted and that 'in-place' means modifying the input array without using extra space. Mention that the solution generalizes to 'at most k duplicates' by comparing with write-k.

1. Understand the problem and constraints

Restate the problem: remove duplicates in-place so each unique element appears at most twice, return the new length. Emphasize O(n) time and O(1) space.

2. Choose the two-pointer approach

Explain that since the array is sorted, duplicates are adjacent. Use a write pointer to overwrite invalid elements and a read pointer to scan.

3. Define the condition for keeping an element

For each element at read, check if write < 2 or if the element differs from the element at write-2. If so, copy it to write and increment write.

4. Walk through an example

Trace the algorithm on a sample array like [1,1,1,2,2,3] to show how the write pointer advances and the array is modified.

5. Analyze complexity and edge cases

State that time is O(n) because each element is read once, and space is O(1) since only two pointers are used. Discuss edge cases like empty array or array with length <=2.

Key Points to Mention

  • Two-pointer technique (read and write pointers)
  • In-place modification without extra space
  • Condition: allow at most two duplicates by comparing with element at write-2
  • Time complexity O(n) and space complexity O(1)
  • Generalization to 'at most k duplicates' by comparing with write-k
  • Handling edge cases: empty array, array length <= 2, all elements same

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