Count Soldiers with a Next-Rank Superior
Updated
Problem statement
Each entry of ranks is the rank of one soldier. A soldier of rank x can report to any soldier of rank exactly x + 1.
A superior may receive reports from any number of soldiers. Count the soldiers who have at least one eligible superior in the array. Count soldiers individually, including repeated ranks.
Function
solution(ranks: int[]) → intExamples
Example 1
ranks = [3,4,3,0,2,2,3,0,0]return = 5The three rank-three soldiers can report to rank four; the two rank-two soldiers can report to rank three. Rank zero has no rank-one superior.
Example 2
ranks = [4,2,0]return = 0No rank is exactly one less than another rank in the array.
Example 3
ranks = [4,4,3,3,1,0]return = 3Two rank-three soldiers have rank-four superiors, and rank zero has a rank-one superior.
Constraints
2 <= ranks.length <= 100000.- For this exercise, assume
0 <= ranks[i] <= 10^9.
More Marshall Wace Online Assessment questions
- Count Letters with Ordered Lowercase and Uppercase OccurrencesOA · Seen Oct 2025
- Maximum Trailing Zeros on a Path with at Most One TurnOA · Seen Oct 2025
- Shortest Subarray Covering an Integer RangeOA · Seen Oct 2025
- Maximum Events in a 60-Second WindowOA · Seen Aug 2025
- Successful Logins from Multiple CountriesOA · Seen Aug 2025
- Maximize Remaining Item Types After Clearing Consecutive ShelvesOA · Seen Nov 2024
- Shortest String After Erasing Equal PairsOA · Seen Nov 2024
- Count Distinct Tags in a Fixed-Radius Circle (Practice Reconstruction)OA · Seen Apr 2024
- Generate Unique Company Email AddressesOA · Seen Apr 2024
- Reconstruct a Two-Row Binary MatrixOA · Seen Apr 2024
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
- Items SortAkuna Capital · OAAkuna Capital interview questions
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Array Rank TransformTata Consultancy Services · OATata Consultancy Services interview questions
- Find Max Health SumAmazon · OA
- Influencers SquadTikTok · OATikTok interview questions
- Count Teams Outscoring a Target TeamBloomberg LP · OABloomberg LP interview questions
- Sort by Frequency, Then ValueC3.ai · OAC3.ai interview questions