Count Increasing Triplets
Updated
Problem statement
Given an array of integers arr, return the number of index triplets (i, j, k) such that i < j < k and arr[i] < arr[j] < arr[k].
Triplets are counted by their indices, so equal values at different positions can form different triplets. The comparisons are strict: two equal values can never be next to each other in a triplet. If no such triplet exists, return 0.
Function
countIncreasingTriplets(arr: int[]) → longExamples
Example 1
arr = [1,2,3,4]return = 4Any three of the four values, kept in their order, are increasing. There are 4 ways to choose three of them: (1, 2, 3), (1, 2, 4), (1, 3, 4) and (2, 3, 4).
Example 2
arr = [4,1,3,2,5]return = 2The value 5 is last and larger than everything, so it is the third value of every triplet here. Before it, the increasing pairs are (1, 3) and (1, 2). So the triplets are (1, 3, 5) and (1, 2, 5), and the answer is 2.
Example 3
arr = [5,5,5,6]return = 0A triplet needs a strictly increasing middle step. Every triplet here starts with two copies of 5, and 5 < 5 is false, so the answer is 0.
Constraints
1 <= arr.length <= 3000-10^9 <= arr[i] <= 10^9- The answer can be larger than
2^31 - 1.
More Salesforce Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
- Flatten Nested Map Paths with a StackONSITE INTERVIEW · Seen Sep 2026
- LFU CacheONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Element in an ArrayONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Merge Overlapping IntervalsAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- First and Last Target Position in a Mountain ArrayGoogle · ONSITE INTERVIEWGoogle interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Round-Robin Iterator over IteratorsBridge · ONSITE INTERVIEWBridge interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions