Bubble Sort

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

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

The Hartford Financial Services OA reported in March 2019 hands you a rule that sounds insulting: sort the array, but don't touch a built-in sort. You have to swap neighbors on each left-to-right pass until nothing is out of order. It's a sorting question with a leash on it. That's the whole test. If the invite is sitting in your inbox, you can finish this in minutes if you stay disciplined. And if your mind goes blank on the loop bounds, StealthCoder runs invisibly during the live assessment as a safety net, so one brain freeze doesn't sink you.

The problem

Implement bubbleSort(nums) using bubble sort to return the integers of nums in nondecreasing order.
On each left-to-right pass, compare neighboring elements and swap them when the left value is greater. Continue until the array is sorted. Do not replace bubble sort with a built-in sorting routine or a different sorting algorithm.
You may modify nums in place. Return the resulting sorted array, preserving every occurrence of each input value.

Function
bubbleSort(nums: int[]) → int[]

Examples
Example 1
nums = [5,1,4,2,8]
return = [1,2,4,5,8]
Repeated neighboring swaps place the smaller values before the larger values.
Example 2
nums = [3,-1,3,0]
return = [-1,0,3,3]
Negative values sort first, and both occurrences of 3 remain.
Example 3
nums = [7]
return = [7]
A one-element array is already sorted.

Constraints
1 <= nums.length <= 2000.
-10^9 <= nums[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is plain bubble sort. Use an outer loop for passes and an inner loop that compares nums[j] and nums[j+1], swapping when the left is greater. Each pass pushes the largest remaining value to the end, so the inner bound shrinks by one every pass. The pitfall is off-by-one: run j up to n-i-2 and you won't read past the array. Add a swapped flag and break early when a pass makes no swaps. That gives O(n) on sorted input and O(n^2) worst case, fine for 2000 elements. Use a strict greater-than so equal values never swap, which keeps duplicates intact. Don't call a built-in sort or the answer gets rejected. If you blank on the bounds mid-assessment, StealthCoder can surface the loop structure while the proctor sees nothing.

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 Bubble Sort 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 Hartford Financial Services's OA.

Hartford Financial Services 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.

Bubble Sort FAQ

How hard is the Hartford Bubble Sort question really?+

Easy. It's a textbook algorithm with no hidden twist. The only risk is sloppy loop bounds or reaching for a built-in sort, which the statement bans. If you can write two nested loops and a swap, you can pass this.

What's the trick to getting it right?+

Compare neighbors, swap when left is greater, and shrink the inner loop each pass because the largest value is already placed at the end. Add a swapped flag to exit early. Use strict greater-than so duplicates stay in order.

Can I use Arrays.sort or sorted() here?+

No. The problem says not to replace bubble sort with a built-in routine or a different algorithm. Even if the output matches, you're ignoring an explicit rule. Write the swap loop by hand.

Will O(n^2) time out with 2000 elements?+

No. Worst case is about 4 million comparisons for 2000 items, which is small. The early-exit flag helps on nearly sorted input, but you don't need anything fancier than standard bubble sort.

How do I prep for this in 48 hours?+

Write bubble sort from memory three times, once with the swapped flag. Test on [5,1,4,2,8], [3,-1,3,0], and a single element. Check negatives and duplicates. That covers every example and edge case in the statement.

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

OA at Hartford Financial Services?
Invisible during screen share
Get it