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

  • 20Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Binary Search Basics
Binary search requires the input array to be
.

20 questions

  1. 1
    Binary search requires the input array to be
    _____
    .
    Hint

    Think about the property that allows the algorithm to eliminate half the array each step.

    Answer

    sorted

  2. 2
    If the target value is found in the array, binary search returns its
    _____
    .
    Hint

    What does the algorithm return when it finds the target?

    Answer

    index

  3. 3
    If the remaining search range becomes empty, binary search returns
    _____
    as the answer.
    Hint

    What value is commonly used to indicate 'not found'?

    Answer

    -1

  4. 4
    The initial search range for an array of size nnn (0-indexed) is from L=0L = 0L=0 to $R =
    _____
    .
    Hint

    What is the index of the last element?

    Answer

    n−1n - 1n−1

  5. 5
    The correct formula to compute the middle index to avoid overflow is $M = L + \frac{
    _____
    }{2}$.
    Hint

    Think about a safe way to compute the average without adding L and R directly.

    Answer

    R−LR - LR−L

  6. 6
    The incorrect formula M=(L+R)/2M = (L+R)/2M=(L+R)/2 can cause an
    _____
    when LLL and RRR are large.
    Hint

    What happens when the sum of two large integers exceeds the maximum value that the data type can hold?

    Answer

    overflow

  7. 7
    In binary search, when a[mid]<targeta[mid] < targeta[mid]<target, the new search range becomes L=mid+1L = mid + 1L=mid+1 and RRR remains the
    _____
    .
    Hint

    Which bound changes when we move to the right half?

    Answer

    same

  8. 8
    The time complexity of binary search in the worst case is
    _____
    .
    Hint

    How does the size of the search space change each step?

    Answer

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

  9. 9
    To check if XXX is a perfect square without using the square root function, we can binary search the
    _____
    .
    Hint

    What value, when squared, should equal X?

    Answer

    square root

  10. 10
    In the lower bound problem, we use a variable
    _____
    to store the best answer found so far.
    Hint

    What is the common name for a variable that holds the candidate answer?

    Answer

    ans

  11. 11
    In lower bound, when a[mid]>=targeta[mid] >= targeta[mid]>=target, we set ans=midans = midans=mid and then search the
    _____
    part for a potentially better answer.
    Hint

    If the current element satisfies the condition, where might a smaller (better) element be?

    Answer

    left

  12. 12
    In lower bound, if no element satisfies the condition, the function returns
    _____
    (or some sentinel value).
    Hint

    What value is returned when no element meets the condition?

    Answer

    -1

  13. 13
    In the rotated array problem, we compare the middle element with the
    _____
    element to decide which half to search.
    Hint

    Which element helps determine if we are in the left (larger) or right (smaller) part?

    Answer

    last

  14. 14
    In the rotated array, if a[mid]>a[last]a[mid] > a[last]a[mid]>a[last], the minimum is in the
    _____
    half.
    Hint

    Which side contains elements smaller than a[mid]?

    Answer

    right

  15. 15
    For the peak element (mountain array) problem, the condition to check is whether $a[m] >
    _____
    $.
    Hint

    Comparing with which neighbor tells us if we are on the increasing slope?

    Answer

    a[m−1]a[m-1]a[m−1]

  16. 16
    In binary search on real numbers, we stop when the distance between L and R is less than a specified
    _____
    .
    Hint

    What term describes the allowed error tolerance?

    Answer

    epsilon

  17. 17
    When performing binary search on real values, we update L=mL = mL=m and R=mR = mR=m instead of using ±1\pm 1±1 because
    _____
    .
    Hint

    Why can't we use integer-style updates with real numbers?

    Answer

    the values are continuous

  18. 18
    Many hard binary search problems can be solved by thinking in terms of a prefix of false and suffix of true, and finding the
    _____
    between them.
    Hint

    What do you look for when the array can be split into two regions based on a condition?

    Answer

    boundary

  19. 19
    The lower bound problem finds the
    _____
    element that is >= X.
    Hint

    Which occurrence of such an element is needed?

    Answer

    first

  20. 20
    In the standard binary search for a target value, if a[mid]>targeta[mid] > targeta[mid]>target, the new search range becomes LLL unchanged and $R =
    _____
    .
    Hint

    Which side do you discard when the middle element is too large?

    Answer

    mid−1mid - 1mid−1

1 of 20
Question 1 of 20

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent