What is the Big-O time complexity of binary search?

Prepare for the FAST Enterprises IC Interview. Enhance your skills with flashcards and multiple-choice questions. Each question provides hints and detailed explanations. Excel in your interview!

Multiple Choice

What is the Big-O time complexity of binary search?

Explanation:
Binary search cuts the search space in half with every comparison. Start with n elements; after k comparisons, at most n/2^k elements remain possible. To reduce the candidates down to one, you need n/2^k ≤ 1, which means 2^k ≥ n and thus k ≥ log2(n). So the worst-case number of comparisons—and thus the time complexity—grows proportionally to log n. The base of the logarithm doesn’t matter in Big-O terms, since different bases differ only by a constant factor. This is why binary search is not linear (O(n)) and not the heavier O(n log n).

Binary search cuts the search space in half with every comparison. Start with n elements; after k comparisons, at most n/2^k elements remain possible. To reduce the candidates down to one, you need n/2^k ≤ 1, which means 2^k ≥ n and thus k ≥ log2(n). So the worst-case number of comparisons—and thus the time complexity—grows proportionally to log n. The base of the logarithm doesn’t matter in Big-O terms, since different bases differ only by a constant factor. This is why binary search is not linear (O(n)) and not the heavier O(n log n).

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy