← Bytedance Interview Insights

Bytedance·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Bytedance coding round, one algorithmic problem, stack-based. Not a lot of fluff, just got handed the problem and expected to solve it efficiently.

Questions Asked (1)

Q1

Given an array of people's heights standing in a queue, for each person determine how many others ahead of them they can actually see. A person is visible only if nobody between them is taller than or equal to both. Return the counts as an array.

Algorithms & Data Structures
Author's notes

The naive approach is obvious and wrong for any real input size.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a monotonic decreasing stack to efficiently compute visible people for each person. Traverse the queue from front to back, maintaining a stack of heights that are strictly decreasing; for each person, pop all shorter or equal heights (they are blocked) and the number of remaining stack elements is the count of visible people. Push the current height onto the stack.

Pro tip: Clarify the visibility condition upfront: a person can see someone ahead if all people between them are strictly shorter than the shorter of the two. This avoids off-by-one errors and demonstrates careful reading.

1. Understand the visibility condition

Restate the problem: person i can see person j (j < i) if for all k with j < k < i, height[k] < min(height[i], height[j]). This means the line of sight is blocked by anyone taller than or equal to the shorter person.

2. Choose the right data structure

Recognize that a monotonic stack (strictly decreasing) is ideal because it maintains candidates that are not blocked by taller people. The stack size at any point represents the number of visible people for the current person.

3. Define the algorithm

Iterate through the queue from left to right. For each person, pop from the stack while the top is less than or equal to the current height. The number of remaining elements in the stack is the count of visible people. Then push the current height.

4. Handle edge cases

Consider empty array, single person, all equal heights, and strictly increasing/decreasing heights. Ensure the algorithm correctly returns 0 for the first person and handles duplicates as per the condition.

5. Analyze complexity and test

State that each element is pushed and popped at most once, giving O(n) time and O(n) space. Walk through a small example to verify correctness.

Key Points to Mention

  • Monotonic stack (strictly decreasing) to maintain visible candidates
  • Time complexity O(n) and space complexity O(n)
  • Visibility condition: all people between must be strictly shorter than the shorter of the two
  • Handling duplicates: pop equal heights because they block visibility
  • Edge cases: empty array, single element, all equal heights
  • Comparison with brute-force O(n^2) approach to highlight efficiency

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