Reported March 2026
Swiggybinary search

First and Last Occurrence

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

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

A linear scan passes the examples and then dies on the hidden tests, which is exactly why Swiggy put this one in front of candidates in March 2026. The array is sorted, and that's the whole hint. If you've got the OA in a day or two, this is a binary search problem dressed up as an array lookup. You need the first and last index of a target, or [-1, -1] if it's missing. It's short, it's fair, and the edge cases are where people lose points. StealthCoder sits invisibly as a safety net if your mind goes blank mid-assessment.

The problem

You are given a sorted integer array nums and an integer target.
Return an array [first, last], where first is the first index where target appears and last is the last index where target appears.
If target does not appear in nums, return [-1, -1].

Function
findFirstAndLastOccurrence(nums: int[], target: int) → int[]

Examples
Example 1
nums = [5, 7, 7, 8, 8, 10]
target = 8
return = [3, 4]
The value 8 first appears at index 3 and last appears at index 4.
Example 2
nums = [5, 7, 7, 8, 8, 10]
target = 6
return = [-1, -1]
The target 6 is not present in the array.

Constraints
nums is sorted in non-decreasing order.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that sorted input means O(log n) is expected, so a full scan is the wrong answer even if it's correct. Run binary search twice. The first pass, when you find target, records the index and keeps searching left (hi = mid - 1). The second pass records the index and keeps searching right (lo = mid + 1). If the first pass finds nothing, return [-1, -1] immediately. The common pitfall is a plain binary search that lands on any match, then expanding outward linearly. That degrades to O(n) when the array is all the same value. Other traps: empty array, single element, and off-by-one in loop bounds. Use lo <= hi consistently and compute mid as lo + (hi - lo) / 2. If you blank on the boundary logic during the live OA, StealthCoder is the hedge that gives you the clean two-pass template without anyone seeing it.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill First and Last Occurrence 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder
⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as find first and last position of element in sorted array. If you have time before the OA, drill that.

⏵ The honest play

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

Swiggy reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

First and Last Occurrence FAQ

How hard is the Swiggy First and Last Occurrence question really?+

Easy to medium. The idea is simple, but the boundary handling trips people up. If you've written binary search before, you can finish it in 10 to 15 minutes. The difficulty is in not falling back to a linear scan or expanding outward from a hit.

What's the trick to solving it?+

Do two binary searches. One biases left to find the first index, the other biases right to find the last. When you hit the target, record the index and keep moving in the biased direction instead of returning. That gives O(log n) even with many duplicates.

Why not just scan the array once?+

It returns correct answers, but it's O(n) and ignores the sorted constraint. The input being sorted is the signal that the intended solution is logarithmic. Hidden tests with large arrays can punish a linear approach, so treat the scan as a fallback only.

What edge cases should I test before submitting?+

Test an empty array, a single element that matches, a single element that doesn't, a target smaller than everything, a target larger than everything, and an array that's all the same value. Also check a target that falls between two existing values. All should return correct indices or [-1, -1].

How do I prepare for this in 48 hours?+

Write the lower-bound and upper-bound binary search templates from memory until they're automatic. Then run them against the two examples and the edge cases above. Don't memorize this single problem. Memorize the left-biased and right-biased pattern, since it shows up in many variants.

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

OA at Swiggy?
Invisible during screen share
Get it