0
0
Intro to Computingfundamentals~20 mins

Searching algorithms (linear, binary) in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Searching Algorithms
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Linear Search Steps

Imagine you have a list of 7 books on a shelf, and you want to find a specific book by its title. You start checking each book from the left until you find the one you want.

Which of the following best describes the number of checks you might do in the worst case?

AYou check half the books, so about 3 or 4 checks.
BYou check only the middle book, so 1 check.
CYou check every book until the last one, so up to 7 checks.
DYou check books randomly until you find it.
Attempts:
2 left
💡 Hint

Think about how linear search works by checking each item one by one.

trace
intermediate
2:00remaining
Trace Binary Search on a Sorted List

Given the sorted list [2, 4, 6, 8, 10, 12, 14], you want to find the number 10 using binary search.

How many comparisons does binary search make before finding 10?

A3 comparisons
B1 comparison
C2 comparisons
D4 comparisons
Attempts:
2 left
💡 Hint

Binary search splits the list in half each time and compares the middle element.

Comparison
advanced
2:00remaining
Comparing Efficiency of Linear vs Binary Search

You have two lists of 1,000 numbers each. One list is unsorted, the other is sorted. You want to find if the number 500 exists.

Which statement correctly compares the number of checks each search method might do?

ABinary search works only on unsorted lists, so it checks all items.
BLinear search on unsorted list may check up to 1,000 items; binary search on sorted list checks about 10 items.
CBoth searches check about 500 items on average.
DLinear search on unsorted list checks about 10 items; binary search on sorted list checks 1,000 items.
Attempts:
2 left
💡 Hint

Recall how binary search halves the search space each time.

identification
advanced
2:00remaining
Identify the Error in Binary Search Implementation

Look at this Python code snippet for binary search on a sorted list:

def binary_search(arr, target):
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

What is the main error that will cause a runtime problem?

AThe high index should be len(arr) - 1, not len(arr).
BThe mid calculation should use (low - high) // 2.
CThe loop condition should be while low < high, not <=.
DThe function should return None instead of -1 when not found.
Attempts:
2 left
💡 Hint

Think about valid index ranges in Python lists.

🚀 Application
expert
2:00remaining
Determine Output of Mixed Search Algorithm

Consider this Python code that searches for a number in a list:

def mixed_search(arr, target):
    if arr != sorted(arr):
        # Use linear search
        for i, val in enumerate(arr):
            if val == target:
                return i
        return -1
    else:
        # Use binary search
        low, high = 0, len(arr) - 1
        while low <= high:
            mid = (low + high) // 2
            if arr[mid] == target:
                return mid
            elif arr[mid] < target:
                low = mid + 1
            else:
                high = mid - 1
        return -1

print(mixed_search([3, 1, 4, 2], 4))

What is the output when running this code?

A-1
B0
C3
D2
Attempts:
2 left
💡 Hint

Check if the list is sorted and which search method runs.