Count Decreasing Triplets
Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Airbnb OA reported in March 2019 looks like a triplet brute force, but it really reduces to one question: for each middle element, how many bigger values sit to its left and how many smaller values sit to its right? Multiply those two counts, sum across every index, and you're done. With n up to 200000, the naive O(n^3) loop is dead on arrival, so you need a counting structure. If you blank on the structure mid-assessment, StealthCoder runs invisibly as a safety net and hands you the approach in real time.
The problem
Given an integer array nums, count the index triplets (i, j, k) such that i < j < k and nums[i] > nums[j] > nums[k]. Return the number of strictly decreasing subsequences of length three. Equal values do not satisfy either strict inequality. Function countDecreasingTriplets(nums: int[]) → long Examples Example 1 nums = [9,4,6,3,2] return = 7 The valid value triples are (9,4,3), (9,4,2), (9,6,3), (9,6,2), (9,3,2), (4,3,2), and (6,3,2). Example 2 nums = [5,4,3,2] return = 4 Every choice of three indices is strictly decreasing, so the answer is C(4,3) = 4. Example 3 nums = [3,3,2,1] return = 2 Either occurrence of 3 can precede 2,1. The two equal 3s cannot form a strict pair with each other. Constraints 0 <= nums.length <= 200000. -10^9 <= nums[i] <= 10^9. The answer fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is fixing the middle index j. Triplets through j equal (count of i < j with nums[i] > nums[j]) times (count of k > j with nums[k] < nums[j]). Get both counts in O(log n) each with a Fenwick tree over coordinate-compressed values. Sweep left to right for the greater-on-left counts, then sweep right to left for the smaller-on-right counts. Total is O(n log n). Pitfalls: equal values must be excluded on both sides, so query strictly greater and strictly smaller. Use a 64-bit accumulator, since the answer can reach about 1.3e15 for 200000 descending elements. Handle the empty array and arrays under length 3, which return 0. Don't skip compression, because values go up to 10^9 in magnitude. If the Fenwick indexing trips you up live, StealthCoder is the hedge that gets you unstuck without the proctor seeing anything.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Count Decreasing Triplets 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count number of teams. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Airbnb's OA.
Airbnb reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Decreasing Triplets FAQ
What's the trick for Count Decreasing Triplets?+
Fix the middle element. For each index j, count larger values to its left and smaller values to its right, then multiply. Summing those products over all j gives the answer. A Fenwick tree makes each count O(log n), so the whole thing runs in O(n log n).
How hard is this Airbnb OA question really?+
Medium to medium-hard. The idea is short, but you need a Fenwick tree or similar structure plus coordinate compression to hit the 200000 limit. Candidates who only know the O(n^2) approach will time out on large inputs.
Why can't I just use O(n^2)?+
With nums.length up to 200000, O(n^2) means around 4e10 operations, which is far too slow. The per-middle-element counting needs to be logarithmic. That's why a Fenwick tree or a merge-sort-style counting approach is the expected solution.
How do I handle equal values correctly?+
Both inequalities are strict. When counting left, query only values strictly greater than nums[j]. When counting right, query only values strictly smaller. Example 3, [3,3,2,1], returns 2 because the two 3s never pair with each other.
How do I prepare for this in 48 hours?+
Write a Fenwick tree from memory twice, including coordinate compression via sorting and a rank map. Then solve this problem using two sweeps. Test on the three given examples, an empty array, and a strictly descending array to confirm 64-bit safety.