← All questions
MediumCoding

132 Pattern

Asked atIntuit
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01State the target: find indices i < j < k with nums[i] < nums[k] < nums[j].
  2. 02Scan from right to left, maintaining a decreasing stack of possible middle values nums[j].
  3. 03Track a separate variable ak: the best valid third value popped from the stack so far.
  4. 04Before pushing the current value, return true if it is smaller than ak.
  5. 05Pop every smaller stack value into ak, then push the current value and explain the invariant.

Reference answer

Then expect these follow-ups

  • Why must the traversal be right to left for this invariant?

    Tests: invariant reasoning

  • How would you return the actual three indices rather than a boolean?

    Tests: state reconstruction

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question