Top N Longest Sentences
Updated
Problem statement
You are given an array of ASCII strings sentences and an integer n. Rank the strings by decreasing character count. If two strings have the same length, the one that appeared earlier in the input ranks first.
Return the first n strings in that ranking.
Function
topLongestSentences(sentences: String[], n: int) → String[]Examples
Example 1
sentences = ["short","the longest one","medium text","tiny"]n = 2return = ["the longest one","medium text"]These are the two strings with the greatest character counts.
Example 2
sentences = ["bbb","aa","ccc","d"]n = 2return = ["bbb","ccc"]bbb and ccc tie at length 3, so their original order is preserved.
Constraints
1 <= sentences.length <= 1000001 <= n <= sentences.length0 <= sentences[i].length <= 1000- Every string contains only printable ASCII characters.
- The sum of all string lengths is at most
100000.
More Deloitte Online Assessment questions
- Seventh Nearest PalindromeOA · Seen May 2026
- Minimum Deletions for a Non-PalindromeOA · Seen Jul 2025
- Minimum-Cost Solar Rooftop AllocationOA · Seen Jul 2025
- Pair Elements with Even SumsOA · Seen Jul 2025
- Count Divisible PermutationsOA · Seen Jan 2025
- Largest Element in an ArrayOA · Seen Nov 2024
- Install Carbon FiltersOA · Seen Oct 2023
- Longest Substring with Even OccurrencesOA · Seen Oct 2023
- Maximum Sum RectangleOA · Seen Apr 2022
- Annual Bank Balance with Monthly Card FeesOA · Seen Jan 2022
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Frequently Bought TogetherAmazon · OA
- Aggregate Paginated Project Run RecordsReplit · OA
- Product Record SearchWells Fargo · OAWells Fargo interview questions
- Sort Ancestral NamesRoblox · OARoblox interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Parse and Sort Integer DataLegora · PHONE SCREEN