In the typical linear search pseudocode, what value is returned if the target is not found?

Study for the End of Year 8 Computer Science Test. Prepare with flashcards and multiple choice questions, each with hints and explanations. Get ready for your exam!

Multiple Choice

In the typical linear search pseudocode, what value is returned if the target is not found?

Explanation:
When a linear search doesn’t find the target, it returns a signal value that isn’t a valid position in the array. Since valid indices range from 0 to length minus 1, using -1 as the return value works perfectly as that signal. It’s a clear, unmistakable indicator that the search failed, because -1 can never be a real index. This makes the follow-up check simple: if the result is -1, the target isn’t present; otherwise, the result tells you where it is. Other options don’t fit as cleanly: returning 0 could be mistaken for a found target at the first position, returning the array length isn’t a valid index, and while some languages use None, -1 is the conventional sentinel in this pseudocode style.

When a linear search doesn’t find the target, it returns a signal value that isn’t a valid position in the array. Since valid indices range from 0 to length minus 1, using -1 as the return value works perfectly as that signal. It’s a clear, unmistakable indicator that the search failed, because -1 can never be a real index.

This makes the follow-up check simple: if the result is -1, the target isn’t present; otherwise, the result tells you where it is. Other options don’t fit as cleanly: returning 0 could be mistaken for a found target at the first position, returning the array length isn’t a valid index, and while some languages use None, -1 is the conventional sentinel in this pseudocode style.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy