← Bytedance Interview Insights

Bytedance·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Bytedance SWE interview, one coding round focused on array manipulation. Pretty standard stuff but the follow-up tripped me up a bit.

Questions Asked (1)

Q1

Given a sorted integer array, modify it in place so each unique value appears at most twice and return the new length. Order must be preserved.

Algorithms & Data Structures
Author's notes

Two pointers.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique: one pointer to scan the array and another to track the position for writing the next valid element. Since the array is sorted, duplicates are adjacent, so you can allow at most two occurrences by comparing the current element with the element two positions before the write pointer.

Pro tip: Clarify that the solution runs in O(n) time and O(1) extra space, and mention that the same pattern generalizes to allowing at most k duplicates by comparing with the element k positions back.

1. Clarify requirements and edge cases

Confirm that the array is sorted, modification is in-place, and the order of unique elements must be preserved. Discuss edge cases like empty array, length 1, or all elements identical.

2. Choose the two-pointer strategy

Explain that you'll use a read pointer to iterate through the array and a write pointer to place the next valid element. This avoids extra space and maintains O(n) time.

3. Define the condition for writing

For each element at the read pointer, write it to the write pointer only if the write pointer is less than 2 (first two elements) or the current element is greater than the element at write pointer - 2. This ensures at most two duplicates.

4. Implement and update pointers

Iterate through the array, apply the condition, and increment the write pointer when a write occurs. Return the write pointer as the new length.

5. Test with examples and analyze complexity

Walk through a few examples (e.g., [1,1,1,2,2,3] -> length 5) to verify correctness. State that time complexity is O(n) and space complexity is O(1).

Key Points to Mention

  • Two-pointer technique (read and write pointers)
  • In-place modification without extra space
  • Leveraging sorted property to detect duplicates
  • Condition: allow at most two occurrences by comparing with element two positions before write pointer
  • Time complexity O(n) and space complexity O(1)
  • Generalization to at most k duplicates

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