Reported July 2026
TCSsorting

Array Rank Transform

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 Array Rank Transform question, reported in July 2026, looks like a ranking problem but it's really dedupe, sort, lookup. That's the whole thing. If you've got an OA invite and 48 hours, this is one of the friendlier ones you could draw. You take distinct values, order them, assign 1, 2, 3 and so on, then map each original element to its rank. The risk isn't difficulty. It's rushing and botching ties or gaps. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes blank on the mapping step.

The problem

You are given an integer array arr. Replace each value with its rank after sorting all distinct values in increasing order.
The smallest distinct value has rank 1. Equal values must receive the same rank, and ranks must be consecutive with no gaps.
Return the transformed array in the original order.

Function
arrayRankTransform(arr: int[]) → int[]

Examples
Example 1
arr = [40,10,20,30]
return = [4,1,2,3]
The sorted distinct values are 10,20,30,40, so their ranks are 1,2,3,4.
Example 2
arr = [100,100,100]
return = [1,1,1]
All values are equal, so all positions receive the same smallest rank.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Copy the array, sort it, and walk through it assigning ranks only when the value changes. Store each value and its rank in a hash map. Then loop over the original array and replace every element with map[value]. Time is O(n log n) from the sort, space is O(n). The common pitfall is ranking by sorted index instead of distinct index, which creates gaps. With [100,100,100] you must return [1,1,1], not [1,2,3]. Another slip is sorting the input in place and losing the original order you need to return. Empty arrays should return empty, so check that. A set plus sort works too, but the single pass over the sorted copy is cleaner. If you freeze during the live TCS OA, StealthCoder can hand you the dedupe-and-map skeleton so you only have to verify the edge cases.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Array Rank Transform 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

TCS reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Array Rank Transform FAQ

How hard is the TCS Array Rank Transform question really?+

Easy. It's a sort plus a hash map lookup, no clever algorithm needed. Most candidates who stumble do so on ties or on keeping the original order. If you can write a sort and a dictionary in your language, you can finish this in a few minutes.

What's the trick to getting ranks without gaps?+

Only increment the rank when you see a new distinct value in the sorted copy. If the current value equals the previous one, reuse the same rank. Using the sorted index directly is the classic bug, because duplicates would eat up rank numbers and leave holes.

Do I need to sort the original array in place?+

No, and you shouldn't. Sort a copy so the original order stays intact for the final mapping pass. Then build a value to rank map from the sorted copy and rewrite each position of the original array by looking up its value.

What edge cases should I test before submitting?+

Test an empty array, a single element, all equal values like [100,100,100] returning [1,1,1], negative numbers, and an already sorted array. Also try a case with repeated values mixed with unique ones, since that's where gap bugs show up.

How do I prepare for this in 48 hours?+

Write the solution once from scratch in your OA language. Then do two variants: using a set plus sort, and using a single pass over the sorted copy. Know the time complexity, O(n log n), and be ready to explain why the hash map is needed for lookup.

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