← Oracle Interview Insights

Oracle·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Oracle SWE interview with a monotonic stack problem that's basically LC 1944 but extended to count visibility in both directions. Not the hardest round I've had but the twist caught me off guard for a minute.

Questions Asked (1)

Q1

Given an array of distinct heights representing people standing in a line, return an array where each entry is the total number of people that person can see, counting both left and right directions. A person A can see person B if everyone standing strictly between them is shorter than the shorter of A and B.

Algorithms & Data Structures
Author's notes

I knew the one-directional version of this problem so my first instinct was to just write that and call it done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a monotonic stack to efficiently compute the number of visible people to the left and right for each person. For each direction, maintain a stack of indices with heights in decreasing order, popping shorter people and counting them as visible, then add the top of the stack if it exists. Sum the counts from both directions to get the total for each person.

Pro tip: Clarify that the visibility condition is equivalent to finding the nearest greater element in each direction, but with the nuance that all shorter people between are also visible. Emphasize that the monotonic stack approach handles this in O(n) time, which is optimal.

1. Understand the problem and clarify visibility rules

Restate the problem: for each person, count how many people they can see to the left and right. A person can see another if everyone between them is shorter than the shorter of the two. Confirm with the interviewer that the array has distinct heights.

2. Design a monotonic stack solution for one direction

For the left-to-right pass, use a stack that stores indices of people in decreasing order of height. For each person, pop all shorter people from the stack (each popped person is visible), then if the stack is not empty, the top person is also visible (since they are taller). Push the current person onto the stack.

3. Apply the same logic for the right-to-left pass

Repeat the process from right to left to count visible people on the right side. Use a separate stack and accumulate the counts into a result array.

4. Combine counts and handle edge cases

Sum the left and right counts for each person. Consider edge cases: empty array, single person, strictly increasing or decreasing heights. Ensure the algorithm runs in O(n) time and O(n) space.

5. Analyze complexity and test with examples

Explain that each person is pushed and popped at most once per direction, so total time is O(n). Walk through a small example (e.g., [3,1,2]) to verify correctness and discuss potential pitfalls.

Key Points to Mention

  • Monotonic stack technique for finding nearest greater elements
  • Two-pass approach: left-to-right and right-to-left
  • Time complexity O(n) and space complexity O(n)
  • Handling of distinct heights and visibility condition
  • Edge cases: empty array, single element, sorted arrays
  • Comparison with brute-force O(n^2) solution to highlight efficiency

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