Count Bowl Subarrays
Reported by candidates from PhonePe's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The PhonePe OA from July 2026 has a problem called Count Bowl Subarrays, and it looks like a subarray counting mess until you see what it reduces to. Each pair of endpoints (l, r) forms a bowl when everything strictly between them is smaller than the lower endpoint. That's a next-greater-element problem in disguise. If you're taking this OA in the next day or two, the monotonic stack is the whole answer. n goes up to 10^5, so brute force dies. StealthCoder sits invisibly as a hedge if your mind goes blank mid-assessment.
The problem
You are given an integer array nums with distinct elements. A subarray nums[l...r] is a bowl when: Its length is at least 3. The smaller of its two endpoint values is strictly greater than every value between the endpoints. Return the number of bowl subarrays. Function bowlSubarrays(nums: int[]) → long Examples Example 1 nums = [2,5,3,1,4] return = 2 The bowl subarrays are [3,1,4] and [5,3,1,4]. Example 2 nums = [5,1,2,3,4] return = 3 The valid bowl subarrays are [5,1,2], [5,1,2,3], and [5,1,2,3,4]. Constraints 3 <= nums.length <= 10^5 1 <= nums[i] <= 10^9 nums contains distinct values.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: for a bowl (l, r), all interior values must be less than min(nums[l], nums[r]). Take the smaller endpoint, call it index i. Then the other endpoint must be the first element to the left or right of i that is greater than nums[i], and everything between is smaller. So each bowl is counted from its smaller endpoint by pairing with a nearest-greater element. Run a monotonic decreasing stack. When you pop an element because the current value is larger, the popped element is the smaller endpoint and the current one bounds it on the right. If the stack still has an element after the pop, that's the left bound. Count only pairs with length at least 3, so skip adjacent pairs. Distinct values remove tie headaches. The pitfall is returning int instead of long, and miscounting adjacent pairs. StealthCoder is the safety net if the stack logic slips under pressure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Bowl Subarrays 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass PhonePe's OA.
PhonePe reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Bowl Subarrays FAQ
What's the trick in Count Bowl Subarrays?+
Think from the smaller endpoint. Every interior value must be below it, so the other endpoint is a nearest-greater element on one side. A monotonic stack finds those pairs in one pass. Then drop pairs with length under 3.
How hard is this PhonePe OA question really?+
Medium on paper. The difficulty is spotting the nearest-greater reduction. Once you see it, the code is about fifteen lines. Without it, you'll write an O(n^2) or O(n^3) scan that times out at n = 10^5.
Why does the return type say long?+
The count of valid pairs can grow toward n^2 in the worst case, which overflows a 32-bit integer when n is 10^5. Use a 64-bit type for the counter. In Python it doesn't matter, but in Java or C++ it does.
Do I need to handle duplicate values?+
No. The problem guarantees distinct elements, so you don't need tie-breaking rules for equal values. Strict comparisons in the stack are fine, and you can skip the usual duplicate edge cases.
How do I prepare for this in 48 hours?+
Practice next greater element and the monotonic stack until the pop-and-count loop is automatic. Then hand-trace both examples from the problem, [2,5,3,1,4] and [5,1,2,3,4], to confirm your counts of 2 and 3 before submitting.