Reported September 2021
Bloombergtwo pointers

Valid Triangle Number

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Strip away the triangle story and this Bloomberg OA question, reported in September 2021, is a counting problem on a sorted array. You're handed numbers and asked how many index triples satisfy one inequality. That's it. If you see it that way in the first minute, the rest is two pointers and a loop. If you don't, you'll write a triple loop and watch it crawl. Know the reduction before the invite window opens. And if your brain locks mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution while you recover.

The problem

Return the number of index triples i < j < k whose three nonnegative values can form the side lengths of a nondegenerate triangle.

Function
triangleNumber(nums: int[]) → int

Examples
Example 1
nums = [2,2,3,4]
return = 3
The valid side triples are (2,3,4) twice by index and (2,2,3).

Constraints
0 <= nums.length <= 1000.
0 <= nums[i] <= 1000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sort the array first. Once sorted, for a triple a <= b <= c, only a + b > c matters, because the other two inequalities hold automatically. Fix the largest side at index k, walking k from the end down. Put left at 0 and right at k-1. If nums[left] + nums[right] > nums[k], then every index from left to right-1 pairs with right, so add right - left and decrement right. Otherwise move left up. That gives O(n^2) total, fine for n up to 1000. The common pitfall is zeros. A zero value can never help, and the strict inequality already rejects it, so don't special-case it, but do make sure you use > and not >=. Another trap is counting by value instead of by index, which breaks the duplicate example. If you blank during the live OA, StealthCoder is the hedge that gets you the two-pointer code fast.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Valid Triangle Number 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as valid triangle number. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg 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.

Valid Triangle Number FAQ

What's the trick for Valid Triangle Number?+

Sort the array, then check only a + b > c for the largest side c. The other two triangle inequalities are automatic after sorting. Fix c at the end, use two pointers on the prefix, and count whole ranges of pairs at once instead of one pair at a time.

How hard is this one really?+

Medium. The idea is short once you see it, but candidates waste time on a brute-force triple loop or on checking all three inequalities. With n up to 1000, O(n^3) is about a billion operations, so it's too slow. The O(n^2) two-pointer approach is the expected answer.

Do duplicates and zeros break the solution?+

No. You count index triples, so duplicate values count separately, which is why the example with [2,2,3,4] returns 3. Zeros fail the strict a + b > c check on their own, so you don't need special handling as long as the comparison is strict.

What's the time and space complexity I should state?+

Time is O(n^2): sorting is O(n log n), and the two-pointer sweep for each fixed largest side is O(n). Space is O(1) extra beyond the sort, depending on your language's sorting implementation. State that clearly if the interviewer asks.

How do I prepare for this in 48 hours?+

Write the sorted two-pointer solution from scratch twice, then trace [2,2,3,4] by hand until the count of 3 makes sense. Practice explaining why range counting with right - left works. That's enough. Don't spend the time on unrelated pattern lists.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it