

AASorting is a core topic in coding interviews because it tests algorithm analysis, trade-offs, and understanding of recursion and data movement.
At Stripe, you are asked to discuss the time complexity of commonly used sorting algorithms. Explain the best-case, average-case, and worst-case time complexity of major algorithms such as Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, and Heap Sort.
Address these specific points:
You do not need to implement the algorithms in full, but you should be able to compare them clearly, explain the source of their complexity, and mention practical trade-offs such as memory usage, recursion behavior, and performance on nearly sorted input.
Sorting algorithms may behave differently depending on input order and pivot or partition behavior. Interviewers expect you to distinguish guaranteed performance from typical performance.
arr = [1, 2, 3, 4] # Nearly sorted input can improve insertion sort
Algorithms like Merge Sort and Quick Sort split the input into smaller parts, solve subproblems recursively, and combine results. Their complexity depends on how balanced the splits are and how much work is done per level.
def merge_sort(arr):
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
A stable sort preserves the relative order of equal elements. This matters when sorting records by multiple keys, such as sorting employees by department after sorting by name.
records = [(2, 'A'), (2, 'B')] # Stable sort keeps A before B
Some algorithms sort mostly within the input array, while others require auxiliary storage. Space usage is often a deciding factor alongside time complexity.
Insertion Sort performs very well on nearly sorted data, while Quick Sort can degrade badly on poor pivot choices. Understanding input characteristics helps choose the right algorithm.