logo

Question detail

Trace this Python linear search for target = 5 and data = [8, 1, 5, 3]. State the values checked in order, the number of comparisons, and the result of the search. for index in range(len(data)): if data[index] == target: print(index) break

Try the question, check the answer, then read the explanation to understand the curriculum point.

At a glance

Question

Type

practice

Style

Topic

Searching algorithms

Exam-style question

Try this first

Trace this Python linear search for target = 5 and data = [8, 1, 5, 3]. State the values checked in order, the number of comparisons, and the result of the search. for index in range(len(data)): if data[index] == target: print(index) break.

Model answer

What a good answer should say

  • The values checked are 8, then 1, then 5.
  • The search makes 3 comparisons, prints 2, and stops because the target is found at zero-based index 2.

Explanation

Why this works

At index 0, 8 is not equal to 5. At index 1, 1 is not equal to 5.

At index 2, 5 matches the target, so 2 is printed and the break statement ends the search.

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions

No questions are published for this page yet.