Reported September 2026
TikToktwo pointers

Cyclic Shift to a Reverse-Sorted Array

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

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

TikTok reported this one in September 2026, and it looks friendlier than it is. You get an array, a cyclic t-shift, and a target of [n, n-1,..., 1]. The trap is that nums can hold anything from -10^9 to 10^9, so it may not even be a permutation. A solution that only hunts for where the 5 sits will pass the samples and then die on the hidden cases. If you blank during the assessment, StealthCoder is the quiet safety net running on your screen. The real work is one pass and one careful check.

The problem

You are given an integer array nums of length n.
For an integer t with 0 <= t < n, a cyclic t-shift moves the last t elements of nums to the beginning while preserving their order.
Return the value of t that transforms nums into the reverse-sorted array [n, n - 1,..., 1]. If no such shift exists, return -1.

Function
solution(nums: int[]) → int

Examples
Example 1
nums = [3,2,1,5,4]
return = 2
Moving the last two elements to the front produces [5,4,3,2,1], so t = 2.
Example 2
nums = [5,4,3,2,1]
return = 0
The array already equals [5,4,3,2,1], so no shift is needed.
Example 3
nums = [1,2,3]
return = -1
No cyclic shift produces [3,2,1], so the result is -1.

Constraints
For this exercise, assume 1 <= nums.length <= 100000.
For this exercise, assume -10^9 <= nums[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Target is [n, n-1,..., 1], so the answer is fixed by one position. Find the index i where nums[i] == n. Moving the last t elements to the front puts old index n-t at position 0, so the value n must sit at index n-t. That gives t = (n - i) % n. Then verify the whole array: for every j, nums[(j - t + n) % n] must equal n - j. Do not skip verification. That's the pitfall. Duplicates, negatives, values above n, or a missing n all mean -1. Also watch n = 1, where [1] returns 0, and t = 0 when n is at index 0. The check is O(n) time, O(1) extra space. If the modular indexing tangles you up mid-assessment, StealthCoder can hand you the verified version so you can type it in calmly.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Cyclic Shift to a Reverse-Sorted Array 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

TikTok reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Cyclic Shift to a Reverse-Sorted Array FAQ

What's the trick in the TikTok cyclic shift problem?+

Locate where n sits, compute t = (n - i) % n, then verify the entire shifted array against [n, n-1,..., 1]. The location only gives you a candidate. The verification pass decides whether the answer is t or -1. Skipping it is the most common failure.

How hard is this really?+

Easy on the idea, medium on the edge cases. The algorithm is a single linear scan plus a check. Where people lose points is modular indexing, off-by-one errors on t, and arrays that aren't permutations of 1..n.

What edge cases should I test first?+

Test n = 1, an array already equal to [n..1] (answer 0), and an array with no element equal to n. Then try duplicates, negatives, and values larger than n. Each of those must return -1 unless the shifted array matches exactly.

Is the two-pointers pattern really needed here?+

Not strictly. The reported hint is two-pointers, but a single index find plus a modular comparison does the job in O(n). You can also compare by walking two indices, one in nums and one in the target, which feels like two pointers.

How do I prepare in 48 hours?+

Write this solution twice from scratch, once with modular indexing and once by building the shifted array to compare. Run it against the three examples plus n = 1 and a duplicate case. Practice explaining why t = (n - i) % n. That's enough for this one.

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

OA at TikTok?
Invisible during screen share
Get it