Reported September 2026
TCSsorting

Maximum Gap After Sorting

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

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

The TCS OA reported in September 2026 has a maximum gap problem that looks like a free point and isn't. The edge case that breaks a naive solution is the tiny input: one element, where there's no adjacent pair and you must return 0. Negative values and a 200000-length array also show up in the constraints. Sort, scan adjacent pairs, done, right? Mostly. This is a sorting problem at heart, and the details decide whether you pass every hidden test. If you blank under the timer, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.

The problem

Given an integer array nums, sort its values in nondecreasing order and find the maximum difference between consecutive values in that sorted order.
Return 0 when nums contains fewer than 2 values.
Implement maximumGap with the integer-array parameter nums and return the maximum adjacent difference as an int.

Function
maximumGap(nums: int[]) → int

Examples
Example 1
nums = [3,6,9,1]
return = 3
After sorting, the array is [1, 3, 6, 9]. The adjacent gaps are 2, 3, and 3, so the maximum is 3.
Example 2
nums = [10]
return = 0
A single value has no adjacent pair.
Example 3
nums = [-5,-1,-10,4]
return = 5
The sorted array is [-10, -5, -1, 4], with gaps 5, 4, and 5.

Constraints
1 <= nums.length <= 200000
-10^9 <= nums[i] <= 10^9
The answer fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The simple route is to sort nums, then loop from index 1 and track the max of nums[i] - nums[i-1]. That's O(n log n), and with n up to 200000 it's fine. Guard first: if length is less than 2, return 0. The pitfalls are small but real. Values go from -10^9 to 10^9, so a difference between two sorted neighbors can exceed what a 32-bit int holds in intermediate math, even though the problem says the answer fits. Use a long for the subtraction in languages where that matters. Don't compute the gap on unsorted data. Don't forget Example 3 has negatives, where the gap of 5 appears twice. If the OA wants linear time, bucket sort with the pigeonhole idea works: the max gap is at least ceil((max-min)/(n-1)), so track only min and max per bucket. StealthCoder is the hedge if you freeze on that bucket variant live.

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 Maximum Gap After Sorting 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum gap. If you have time before the OA, drill that.

⏵ The honest play

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

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

Maximum Gap After Sorting FAQ

How hard is the TCS maximum gap problem really?+

Easy if you sort first. The statement as written only asks you to sort and find the largest adjacent difference, which is a few lines. The difficulty is in the edge cases: a single element returning 0 and negative numbers. Get those right and it's a quick solve.

What's the trick to this problem?+

Sort the array, then scan adjacent pairs and keep the largest difference. Return 0 up front when there are fewer than 2 values. The optional trick is bucket sort using the pigeonhole principle, which gets you O(n) if linear time is demanded.

Will O(n log n) pass with 200000 elements?+

Yes, in practice. Sorting 200000 integers is well within normal limits in any mainstream language. Nothing in the problem text demands linear time, so the built-in sort plus one pass is a safe answer unless the assessment says otherwise.

Do I need to worry about integer overflow?+

Possibly. Values range from -10^9 to 10^9, so subtracting a negative from a positive can reach 2*10^9, past a signed 32-bit max. The answer is promised to fit, but cast to a 64-bit type for the subtraction in Java or C++ to stay safe.

How do I prepare for this in 48 hours?+

Write the sort-and-scan version from memory, then test it on a single element, duplicates, all negatives, and mixed signs. If you have time left, sketch the bucket approach once. Don't grind more than that, since this is a pattern you can finish in minutes.

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

OA at TCS?
Invisible during screen share
Get it