Maximum Good Triplet Distance
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's November 2024 OA reports include a problem that sounds like a triplet search but really reduces to a visibility check between neighboring elements. You get an array, you pick i < j < k, and everything strictly between each adjacent pair has to be shorter than both ends. With 200000 elements, brute force over triplets is dead on arrival. If you spot the stack structure early, this is short code. If you don't, StealthCoder is the safety net running invisibly during the live OA in case you blank.
The problem
You are given an integer array A. A triplet of indices (i, j, k) is good when i < j < k and both visibility conditions hold: For every index p with i < p < j, A[p] < A[i] and A[p] < A[j]. For every index q with j < q < k, A[q] < A[j] and A[q] < A[k]. Return the maximum possible index distance k - i among all good triplets. Return -1 if no good triplet exists. Function maximumGoodTripletDistance(A: int[]) → int Examples Example 1 A = [5,1,4,2,6] return = 4 The triplet (0, 2, 4) is good. Value 1 is strictly below both 5 and 4, while value 2 is strictly below both 4 and 6. Its distance is 4 - 0 = 4. Example 2 A = [3,3,1,3,3] return = 3 Equal-height endpoints block visibility through one another because every interior value must be strictly smaller. One maximum-span good triplet is (0, 1, 3), with distance 3. Constraints 3 <= A.length <= 200000 -10^9 <= A[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the reduction. The first condition says i and j can see each other: everything between them is strictly smaller than both. The second says j and k can see each other. So you need a middle index j, a left partner i that sees j, and a right partner k that sees j, then maximize k - i. A monotonic stack finds visible pairs in O(n). Pop while the top is smaller than the current value, and each popped or remaining top pairs with the current element. For each j, take the farthest visible index on the left and the farthest on the right. The pitfall is equality. Example 2 shows equal values block visibility, so use strict comparisons and handle duplicates by popping or replacing carefully. Also remember adjacent indices see each other trivially, since nothing lies between them. Return -1 if no j has both sides. StealthCoder is the hedge if the stack invariant slips under pressure.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Good Triplet Distance cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Good Triplet Distance FAQ
What's the trick in Maximum Good Triplet Distance?+
Split it into two visibility relations sharing a middle index j. Left side needs i that sees j, right side needs k that sees j. A monotonic stack enumerates visible pairs in linear time, so you pick the farthest on each side and maximize k - i.
How hard is this really?+
Medium-hard. The wording is dense, but the code is short once you see the stack. The difficulty is the reduction and strict inequality handling, not the implementation. With n up to 200000 you need O(n) or O(n log n).
Why do equal values matter so much?+
Every interior value must be strictly smaller than both endpoints. So equal heights block sight. In Example 2, [3,3,1,3,3], the equal 3s change which pairs are visible. Use strict comparisons and test duplicates before you submit.
Do adjacent indices count as visible?+
Yes. If j = i + 1 there is no p between them, so the condition holds vacuously. That's why small arrays like Example 1 still produce valid triplets. Forgetting this gives wrong -1 answers on short inputs.
How do I prepare in 48 hours for a Google OA like this?+
Practice monotonic stack problems like next greater element and visible-pairs variants. Write the stack by hand twice, including a duplicates case. Then test edge cases: all equal values, strictly increasing, strictly decreasing, and length 3.