1. Home
  2. Explore
  3. Computer Science
  4. Binary Search Algorithm

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Binary Search Algorithm

  • 12Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Basic Binary Search

What is the fundamental idea behind binary search?

12 questions

  1. 1

    What is the fundamental idea behind binary search?

    1. A

      Start from the beginning and compare each element until the target is found

    2. B

      Use a hash table to map values to positions for instant lookup

    3. C

      Divide the array into two halves and search both halves recursively

    4. D

      Repeatedly halve the search range by comparing the middle element with the target

    Correct answer
    D

    Repeatedly halve the search range by comparing the middle element with the target

    Explanation

    Binary search works by repeatedly halving the search range. At each step, the middle element is compared with the target, and the search continues in the left or right half accordingly.

  2. 2

    Why should the formula M=(L+R)/2M = (L + R) / 2M=(L+R)/2 be avoided in binary search implementations?

    1. A

      It always returns a floating-point number, which cannot be used as an index

    2. B

      It always rounds up, causing off-by-one errors

    3. C

      It may cause integer overflow when LLL and RRR are both large, leading to incorrect results

    4. D

      It is slower than using bitwise operations

    Correct answer
    C

    It may cause integer overflow when LLL and RRR are both large, leading to incorrect results

    Explanation

    Adding two large integers can overflow the maximum value representable by the data type, causing the sum to wrap around and produce a negative or incorrect value. The safe alternative is M=L+(R−L)/2M = L + (R - L) / 2M=L+(R−L)/2.

  3. 3

    What is the correct formula to compute the middle index in binary search to avoid overflow?

    1. A

      M=L−(R−L)/2M = L - (R - L) / 2M=L−(R−L)/2

    2. B

      M=(L+R)/2M = (L + R) / 2M=(L+R)/2

    3. C

      M=L+(R−L)/2M = L + (R - L) / 2M=L+(R−L)/2

    4. D

      M=(R−L)/2M = (R - L) / 2M=(R−L)/2

    Correct answer
    C

    M=L+(R−L)/2M = L + (R - L) / 2M=L+(R−L)/2

    Explanation

    Using M=L+(R−L)/2M = L + (R - L) / 2M=L+(R−L)/2 ensures that the difference (R−L)(R - L)(R−L) never exceeds the initial range length, avoiding overflow of L+RL+RL+R.

  4. 4

    In binary search for an exact target in a sorted array, what should you do when the middle element is less than the target?

    1. A

      Set R=mid−1R = \text{mid} - 1R=mid−1

    2. B

      Set L=midL = \text{mid}L=mid

    3. C

      Terminate and return -1

    4. D

      Set L=mid+1L = \text{mid} + 1L=mid+1

    Correct answer
    D

    Set L=mid+1L = \text{mid} + 1L=mid+1

    Explanation

    Since the array is sorted, if a[mid]<targeta[\text{mid}] < \text{target}a[mid]<target, the target must be to the right, so the left boundary is updated to mid+1\text{mid}+1mid+1.

  5. 5

    What is the time complexity of binary search in the worst case?

    1. A

      O(1)O(1)O(1)

    2. B

      O(log⁡N)O(\log N)O(logN)

    3. C

      O(N)O(N)O(N)

    4. D

      O(Nlog⁡N)O(N \log N)O(NlogN)

    Correct answer
    B

    O(log⁡N)O(\log N)O(logN)

    Explanation

    Each step halves the search range, leading to at most log⁡2N\log_2 Nlog2​N steps. Thus the worst-case time complexity is logarithmic.

  6. 6

    When implementing lower bound (first element ≥X\geq X≥X), what is the purpose of the variable ans?

    1. A

      To keep track of the smallest index that satisfies the condition

    2. B

      To count the number of iterations

    3. C

      To store the middle index for recursive calls

    4. D

      To store the initial value of the target

    Correct answer
    A

    To keep track of the smallest index that satisfies the condition

    Explanation

    The variable ans is updated whenever an element satisfies the condition (a[mid]≥Xa[\text{mid}] \geq Xa[mid]≥X), and by continuing to search to the left, we ensure ans holds the first (leftmost) such index.

  7. 7

    In the lower bound problem, if no element satisfies the condition, what value should be returned?

    1. A

      000

    2. B

      The last index of the array

    3. C

      −1-1−1 or a sentinel like infinity

    4. D

      The size of the array

    Correct answer
    C

    −1-1−1 or a sentinel like infinity

    Explanation

    If no element meets the condition, a sentinel value (e.g., −1-1−1 or ∞\infty∞) is returned to indicate that no valid element exists.

  8. 8

    In the rotated sorted array problem (finding the minimum), what condition is used to split the array into prefix (true) and suffix (false)?

    1. A

      a[mid]<a[0]a[\text{mid}] < a[0]a[mid]<a[0]

    2. B

      a[mid]>a[last]a[\text{mid}] > a[\text{last}]a[mid]>a[last]

    3. C

      a[mid]<a[mid−1]a[\text{mid}] < a[\text{mid}-1]a[mid]<a[mid−1]

    4. D

      a[mid]>a[mid+1]a[\text{mid}] > a[\text{mid}+1]a[mid]>a[mid+1]

    Correct answer
    B

    a[mid]>a[last]a[\text{mid}] > a[\text{last}]a[mid]>a[last]

    Explanation

    By comparing the middle element with the last element, elements greater than a[last]a[\text{last}]a[last] belong to the rotated larger part (true), and those ≤a[last]\leq a[\text{last}]≤a[last] belong to the smaller part (false). The first false marks the minimum.

  9. 9

    What is the key insight for applying binary search to problems like finding the peak in a mountain array?

    1. A

      Always compare the middle element with the first element

    2. B

      Use a linear scan to find the peak first

    3. C

      Use recursion without a base case

    4. D

      Look for a condition that creates a prefix of true and suffix of false (or vice versa)

    Correct answer
    D

    Look for a condition that creates a prefix of true and suffix of false (or vice versa)

    Explanation

    The core idea is to define a condition that splits the array into two parts (e.g., increasing then decreasing). Binary search then finds the boundary, which corresponds to the answer.

  10. 10

    When binary searching for the square root of xxx with a given precision, when does the algorithm terminate?

    1. A

      When the range [L,R][L, R][L,R] becomes empty

    2. B

      When mid2\text{mid}^2mid2 exactly equals xxx

    3. C

      When LLL exceeds RRR

    4. D

      When R−LR - LR−L is smaller than the required precision

    Correct answer
    D

    When R−LR - LR−L is smaller than the required precision

    Explanation

    Since exact equality is unlikely with real numbers, the loop continues until the interval length is less than the tolerance, at which point either LLL or RRR is an acceptable approximation.

  11. 11

    In binary search on real numbers, why do we not update boundaries with mid+1\text{mid}+1mid+1 or mid−1\text{mid}-1mid−1?

    1. A

      Because the values are continuous and we need to avoid skipping the answer

    2. B

      Because the array is not sorted

    3. C

      Because we always want to include mid in the next range

    4. D

      Because mid is a floating-point number and indices must be integers

    Correct answer
    A

    Because the values are continuous and we need to avoid skipping the answer

    Explanation

    With real numbers, the answer is a point in a continuous interval. Using mid+1\text{mid}+1mid+1 would skip over potential values; instead, we set L=midL = \text{mid}L=mid or R=midR = \text{mid}R=mid to narrow the interval.

  12. 12

    What was the bug in Java's binary search implementation that went unnoticed for over nine years?

    1. A

      It computed the middle index using (L+R)/2(L+R)/2(L+R)/2, causing overflow

    2. B

      It used an incorrect comparison operator

    3. C

      It returned the wrong index when the target was not found

    4. D

      It did not handle empty arrays

    Correct answer
    A

    It computed the middle index using (L+R)/2(L+R)/2(L+R)/2, causing overflow

    Explanation

    The Java library used M=(L+R)/2M = (L+R)/2M=(L+R)/2, which overflows for large LLL and RRR. This bug was present for more than nine years before being discovered.

Question 1 of 12
Question 1 of 12

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent