Reported February 2019
Deloittesorting

Top N Longest Sentences

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

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

The edge case that breaks a naive Top N Longest Sentences solution is the tie. Deloitte candidates reported this OA in February 2019, and it looks trivial until two strings share a length and your sort scrambles their order. The hinted tag says dynamic programming, but there's no DP here. It's a stable sort by length descending, then take the first n. If you blank on the tie rule or the empty-string case, StealthCoder is the invisible safety net that reads the problem on screen and hands you a working solution during the live OA.

The problem

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 = 2
return = ["the longest one","medium text"]
These are the two strings with the greatest character counts.
Example 2
sentences = ["bbb","aa","ccc","d"]
n = 2
return = ["bbb","ccc"]
bbb and ccc tie at length 3, so their original order is preserved.

Constraints
1 <= sentences.length <= 100000
1 <= n <= sentences.length
0 <= sentences[i].length <= 1000
Every string contains only printable ASCII characters.
The sum of all string lengths is at most 100000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is stability. Sort indices or pairs by length descending and break ties by original index ascending. Then slice the first n. In Python, sorted with key=lambda s: -len(s) is already stable, so ties keep input order. In Java, Collections.sort on objects is stable, but Arrays.sort on primitives isn't, so don't sort a raw int array of lengths. The common pitfall is sorting ascending then reversing the list, which flips the tie order and fails Example 2. Another is forgetting that empty strings are valid and have length 0. Complexity is O(k log k) for k strings, and the total length cap of 100000 keeps memory fine. A heap works too, but needs the index as a tiebreaker. If you freeze in the live OA, StealthCoder is the hedge that gives you the stable-sort version fast.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Top N Longest Sentences 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Deloitte reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top N Longest Sentences FAQ

How hard is Top N Longest Sentences really?+

Easy. It's a sort with a tiebreaker, not a real algorithm puzzle. The only way to fail is mishandling ties or reversing a list after an ascending sort. Write it carefully and test Example 2 before submitting.

What's the trick to the tie rule?+

Use a stable sort by length descending, or sort by the pair (negative length, original index). Either way, equal-length strings keep input order. Don't sort ascending and reverse, because that flips the ties.

Is this really a dynamic programming problem?+

No. The hinted tag is misleading. Nothing here has overlapping subproblems. It's sorting, or a heap if you want to be fancy. Don't waste time hunting for a DP recurrence.

Do I need to worry about performance with 100000 strings?+

Not much. Sorting 100000 items is O(k log k), which is fast. The sum of lengths is capped at 100000, so computing lengths is cheap. Just avoid copying strings repeatedly or doing nested loops.

How do I prepare for this in 48 hours?+

Practice stable sorting with custom keys in your language of choice. Know whether your sort is stable, and how to attach an index as a tiebreaker. Run both examples and an empty-string case by hand. That covers it.

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

OA at Deloitte?
Invisible during screen share
Get it