Katkat

Solve Search Insert Position Using Binary Search

AmazonData Scientist · Technical Screen

Last updated

This question evaluates proficiency in binary search, algorithmic implementation on sorted arrays, and understanding of time complexity and index management.

Scenario

Live coding round for an Amazon Applied Scientist position; candidate must solve LeetCode 35 (Search Insert Position) without helper libraries.

Question

Given a sorted array of distinct integers nums and an integer target, return the index if target exists; otherwise return the index where it would be inserted to keep order. Implement the solution using an explicit binary search (no built-in bisect).

Hints

Iterative or recursive binary search, O(log n) time, manage low/high pointers and insertion position when loop exits.