Binary Search

Help Questions

AP Computer Science Principles › Binary Search

Questions 1 - 10
1

A database stores sorted usernames array "adam", "bella", "carlos", "dina", "eli", "fatima", "gwen", "hugo", "ivan" and searches for target "fatima" using iterative binary search; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$, comparing strings alphabetically. Steps: low=0, high=8, mid=4 ("eli") < "fatima" so low=5; next mid=$\lfloor(5+8)/2\rfloor$=6 ("gwen") > "fatima" so high=5; next mid=$\lfloor(5+5)/2\rfloor$=5 ("fatima") found. Refer to the array provided above. How many iterations are required to locate the target element using binary search?

2 iterations are required.

6 iterations are required.

3 iterations are required.

9 iterations are required.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, a sorted array of strings ["adam", "bella", "carlos", "dina", "eli", "fatima", "gwen", "hugo", "ivan"] is searched for "fatima" using alphabetical comparison. Choice B is correct because exactly 3 iterations are required: first finds "eli" at mid=4, second finds "gwen" at mid=6, and third finds "fatima" at mid=5. Choice C is incorrect because it might represent a miscount or confusion with the index position of the target. To help students: Demonstrate that binary search works identically with strings using alphabetical ordering, and practice with both numeric and string examples. Watch for: students thinking string comparison works differently than numeric comparison, and ensure they understand iteration counting.

2

A nearly complete sorted sequence is stored as array 101, 102, 103, 104, 105, 106, 108, 109, 110, 111; exactly one integer is missing. Students use iterative binary search on the expected range 101–111, using midpoint $\lfloor(\text{low}+\text{high})/2\rfloor$ and comparing arraymid to expected value (101+mid). Steps: low=0, high=9, mid=4: array4=105 matches expected 105 so low=5; mid=$\lfloor(5+9)/2\rfloor$=7: array7=109 but expected 108 so high=6; mid=$\lfloor(5+6)/2\rfloor$=5: array5=106 matches expected 106 so low=6; mid=$\lfloor(6+6)/2\rfloor$=6: array6=108 but expected 107, so missing value is 107. Refer to the array provided above. What is the position of the target element in the array after applying binary search?

Missing value 107 is identified.

No value is missing because the array is sorted.

Missing value 106 is identified.

Missing value 107 after 10 sequential checks.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, binary search is cleverly adapted to find a missing value by comparing actual array values with expected sequential values. Choice A is correct because the algorithm identifies that 107 is missing when array[6]=108 but the expected value at that position should be 107 in a complete sequence. Choice B is incorrect because 106 is present in the array at index 5 - students might misread the array or misunderstand the algorithm. To help students: Explain how binary search can be modified for different problems beyond simple element finding, and practice identifying patterns in sequences. Watch for: confusion about how the expected value is calculated and difficulty understanding this non-standard application of binary search.

3

A student applies iterative binary search on the sorted array (ascending): index 0..9, A=5, 9, 13, 17, 21, 25, 29, 33, 37, 41. Target starts as 33 and ends found. Pseudocode uses mid=(low+high)/2. Steps: mid=4 (21)<33 => low=5; mid=7 (33)=target => stop. Refer to the array provided above. What is the position of the target element in the array after applying binary search?

Position 6.

Position 7.

Position 8.

Position 5.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, we have a 10-element array (indices 0-9) searching for the value 33, which the algorithm successfully locates at index 7. Choice B is correct because it identifies position 7 where the target 33 is found: first iteration calculates mid=4 where A[4]=21<33, adjusting low=5; second iteration calculates mid=7 where A[7]=33 equals our target. Choice A is incorrect because it suggests position 6, which would contain the value 29, not our target 33. To help students: Use array diagrams with indices clearly labeled and practice converting between array indices and positions. Emphasize that in zero-indexed arrays, position numbers match index numbers.

4

A class uses iterative binary search on the sorted string array: index 0..11, S="Ant","Bee","Cat","Deer","Eel","Fox","Goat","Hawk","Ibis","Lion","Mole","Newt". Target starts as "Mole" and ends found. Pseudocode: mid=(low+high)/2; compare strings alphabetically; adjust low/high. Steps: mid=5 ("Fox"<"Mole") => low=6; mid=8 ("Ibis"<"Mole") => low=9; mid=10 ("Mole"=target) => stop. Refer to the array provided above. How many iterations are required to locate the target element using binary search?

12 iterations are required.

4 iterations are required.

2 iterations are required.

3 iterations are required.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, we have a 12-element string array (indices 0-11) searching for "Mole", with the algorithm showing three distinct iterations using alphabetical comparison. Choice B is correct because it counts all three iterations: (1) mid=5, "Fox"<"Mole", adjust low=6; (2) mid=8, "Ibis"<"Mole", adjust low=9; (3) mid=10, "Mole"=target, search completes. Choice A is incorrect because it undercounts the iterations, missing one of the comparison steps needed to locate the target. To help students: Practice with string arrays to reinforce alphabetical ordering concepts. Create trace tables that clearly show each iteration's boundaries, mid calculation, and string comparison result.

5

A student uses iterative binary search on sorted array 5, 8, 12, 17, 21, 26, 30, 34, 41, 49, 55, 63, 70, 81 to find target 34; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=13, mid=6 (30)<34 so low=7; next mid=$\lfloor(7+13)/2\rfloor$=10 (55)>34 so high=9; next mid=$\lfloor(7+9)/2\rfloor$=8 (41)>34 so high=7; next mid=$\lfloor(7+7)/2\rfloor$=7 (34) found. Refer to the array provided above. How many iterations are required to locate the target element using binary search?

7 iterations are required.

14 iterations are required.

4 iterations are required.

3 iterations are required.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the array [5, 8, 12, 17, 21, 26, 30, 34, 41, 49, 55, 63, 70, 81] is searched for 34, requiring students to count iterations carefully. Choice B is correct because exactly 4 iterations are needed: mid=6 (30), mid=10 (55), mid=8 (41), and finally mid=7 (34). Choice A is incorrect because it undercounts by one iteration - students might miss counting the final successful comparison. To help students: Create iteration tracking tables and emphasize that finding the target still counts as an iteration. Watch for: off-by-one errors in counting iterations and confusion about whether the successful find counts as an iteration.

6

An iterative binary search is performed on sorted array 1, 6, 9, 14, 20, 27, 35, 44, 50, 61, 73, 88, 94 to find target 44; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=12, mid=6 (35)<44 so low=7; next mid=$\lfloor(7+12)/2\rfloor$=9 (61)>44 so high=8; next mid=$\lfloor(7+8)/2\rfloor$=7 (44) found. Refer to the array provided above. During the binary search, what is the middle element for the first iteration?

27

35

44

50

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the array [1, 6, 9, 14, 20, 27, 35, 44, 50, 61, 73, 88, 94] has 13 elements, and students must identify the first iteration's middle element. Choice B is correct because with low=0 and high=12, the midpoint calculation ⌊(0+12)/2⌋=6 gives index 6, which contains the value 35. Choice C is incorrect because 44 is the target value at index 7, not the middle element of the first iteration - students may confuse the target with the midpoint. To help students: Always start by calculating the initial midpoint before any comparisons, and use array visualization tools to highlight the middle element. Watch for: confusion between the target value and the middle element value, and errors in initial midpoint calculation.

7

A class uses iterative binary search on sorted array 4, 9, 13, 18, 21, 27, 33, 39, 45, 52, 60 to locate target 45; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=10, mid=5 (27)<45 so low=6; next mid=$\lfloor(6+10)/2\rfloor$=8 (45) found. Refer to the array provided above. During the binary search, what is the middle element for the second iteration?

33

39

45

52

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the array [4, 9, 13, 18, 21, 27, 33, 39, 45, 52, 60] is searched for 45, and students must identify the middle element in the second iteration. Choice C is correct because in the second iteration with low=6 and high=10, the midpoint calculation ⌊(6+10)/2⌋=8 gives index 8, which contains the value 45. Choice B is incorrect because 39 is at index 7, not the calculated midpoint of the second iteration - students may confuse adjacent indices. To help students: Create tables showing low, high, mid values for each iteration, and practice calculating midpoints using integer division. Watch for: off-by-one errors in midpoint calculations and confusion about which iteration is being referenced.

8

A phone directory is alphabetically sorted: "Allen", "Baker", "Chen", "Diaz", "Evans", "Foster", "Gupta", "Hernandez", "Ibrahim", "Jones", "Khan", "Lopez". To find "Jones", iterative binary search uses midpoint $\lfloor(\text{low}+\text{high})/2\rfloor$: low=0, high=11, mid=5 ("Foster") < "Jones" so low=6; mid=$\lfloor(6+11)/2\rfloor$=8 ("Ibrahim") < "Jones" so low=9; mid=$\lfloor(9+11)/2\rfloor$=10 ("Khan") > "Jones" so high=9; mid=$\lfloor(9+9)/2\rfloor$=9 ("Jones") found. Refer to the array provided above. Why is binary search more efficient than linear search in this scenario?

It halves the remaining search range each iteration.

It works best when the array is randomly ordered.

It requires more comparisons as the array grows.

It checks elements sequentially from the first entry.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, a phone directory demonstrates binary search on alphabetically sorted names, highlighting the algorithm's efficiency advantage. Choice A is correct because binary search's key efficiency comes from eliminating half the remaining elements with each comparison, reducing time complexity to O(log n). Choice B is incorrect because it describes linear search behavior - binary search specifically avoids sequential checking by jumping to calculated midpoints. To help students: Compare the number of steps needed for binary vs linear search on the same array, showing how the difference grows with array size. Watch for: confusion between binary and linear search behaviors, and ensure students understand the logarithmic time complexity advantage.

9

A teacher demonstrates iterative binary search on sorted array "ant", "bee", "cat", "dog", "eel", "fox", "goat", "hawk", "ibis", "jaguar" to find target "eel"; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=9, mid=4 ("eel") found immediately. Refer to the array provided above. During the binary search, what is the middle element for the given iteration?

"eel"

"cat"

"fox"

"dog"

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the sorted array of animal names demonstrates an optimal case where the target "eel" is found immediately at the first midpoint. Choice B is correct because with low=0 and high=9, the midpoint calculation ⌊(0+9)/2⌋=4 gives index 4, which contains "eel" - the target is found in just one iteration. Choice D is incorrect because "cat" is at index 2, not the calculated midpoint - students might count positions incorrectly. To help students: Emphasize that binary search can find elements in one iteration if they happen to be at the midpoint, demonstrating best-case scenarios. Watch for: miscounting array positions or assuming binary search always requires multiple iterations.

10

A student runs iterative binary search on the sorted array (ascending): index 0..11, A=2, 5, 9, 13, 18, 21, 27, 33, 38, 44, 49, 56. Target starts as 18 and ends found. Pseudocode uses mid=(low+high)/2. Steps: low=0, high=11 => mid=5 (A5=21)>18 => high=4; low=0, high=4 => mid=2 (A2=9)<18 => low=3; low=3, high=4 => mid=3 (A3=13)<18 => low=4; low=4, high=4 => mid=4 (A4=18)=target. Refer to the array provided above. How many iterations are required to locate the target element using binary search?

5 iterations are required.

12 iterations are required.

3 iterations are required.

4 iterations are required.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, we have a 12-element sorted array (indices 0-11) and are searching for the value 18, with the algorithm clearly showing four distinct iterations. Choice B is correct because it accurately counts all four iterations: (1) mid=5, A[5]=21>18, adjust high=4; (2) mid=2, A[2]=9<18, adjust low=3; (3) mid=3, A[3]=13<18, adjust low=4; (4) mid=4, A[4]=18=target, search completes. Choice A is incorrect because it undercounts the iterations, possibly missing the final iteration where the target is found. To help students: Create a table showing iteration number, low/high values, mid calculation, and comparison result. Emphasize that finding the target still counts as an iteration, and practice with arrays of different sizes to reinforce the logarithmic nature of binary search.

Page 1 of 2