Reported July 2024
Mygatebinary search

Minimum in a Rotated Sorted Array

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

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

Mygate flagged this one in July 2024, and it's a classic hiding in plain sight. The whole solution hinges on a plain array and the fact that it's sorted-then-rotated, which means one side of any midpoint is always cleanly ordered. If you've got a Mygate OA invite, expect to be asked for the minimum of a rotated sorted array in O(log n) time and O(1) space. A linear scan works but fails the stated bound. This is binary search wearing a disguise. StealthCoder sits invisibly as a safety net if you blank on the loop conditions mid-assessment.

The problem

Given a nonempty array nums formed by rotating a strictly increasing integer array, return its minimum element.
Rotating moves a prefix to the end while preserving the order within each part. Zero rotations are allowed. All elements are distinct.
Your solution must use O(log n) time and O(1) extra space.

Function
minimumRotatedArray(nums: int[]) → int

Examples
Example 1
nums = [3,4,5,1,2]
return = 1
The increasing sequence [1, 2, 3, 4, 5] was rotated, and its minimum remains 1.
Example 2
nums = [1,2,3]
return = 1
A zero rotation is valid; the first value is the minimum.
Example 3
nums = [5]
return = 5
A singleton has no different minimum.

Constraints
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
All values are distinct, and nums is a rotation of a strictly increasing array.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: compare nums[mid] to nums[right]. If nums[mid] > nums[right], the minimum sits strictly right of mid, so set left = mid + 1. Otherwise the minimum is at mid or to its left, so set right = mid. Loop while left < right, then return nums[left]. Never compare against nums[left] alone, because the zero-rotation case like [1,2,3] breaks that logic. The common pitfall is using right = mid - 1, which can skip the answer. Also don't worry about duplicates, since the problem guarantees distinct values. Single-element arrays fall out naturally because the loop never runs. Memorize the two-branch update and the left < right condition. If you freeze on those details during the live OA, StealthCoder gives you the clean version without anyone seeing it on screen.

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 Minimum in a Rotated 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 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as find minimum in rotated sorted array. If you have time before the OA, drill that.

⏵ The honest play

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

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

Minimum in a Rotated Sorted Array FAQ

How hard is Minimum in a Rotated Sorted Array really?+

It's a medium on paper but easy once you know it. The code is about eight lines. Most failures come from off-by-one errors in the binary search bounds, not from misunderstanding the problem. Write it once by hand and you'll recognize it instantly.

What's the trick for the O(log n) requirement?+

Compare nums[mid] with nums[right]. If mid is larger, the drop point is to the right, so move left past mid. If not, the minimum is at mid or left of it, so shrink right to mid. That halves the search each step.

Why does right = mid and not mid - 1?+

Because mid itself might be the minimum. If nums[mid] is less than nums[right], mid is a valid candidate and discarding it could lose the answer. Only the left side moves past mid, since there nums[mid] is definitely not the minimum.

Does the zero-rotation case break anything?+

Not if you compare against nums[right]. For [1,2,3], mid is always less than right, so right keeps shrinking until it lands on index 0. Comparing against nums[left] is where people get burned, so avoid it.

How do I prepare for this in 48 hours?+

Write the binary search from memory three times, then test on [3,4,5,1,2], [1,2,3], and [5]. Also try a two-element rotation like [2,1]. If those pass, you're set. Spend the rest of your time on other binary search variants rather than rereading this one.

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

OA at Mygate?
Invisible during screen share
Get it