Find 5 with a loop and show its index.
Acceptance criteria
- 1.#out should show index 2.
DSA Tutorial · Searching
These 10 tasks go with the Linear Search and When to Use It chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Linear search is the one everybody dismisses. On the sizes most code actually deals with, it is frequently the fastest option and always the clearest.” .
1.Scan for a valueexercise · easy · 10 min
Find 5 with a loop and show its index.
Passes when: #out should show index 2.
2.Exit earlyexercise · easy · 10 min · practice plan
Count how many elements are examined before finding the first match.
Passes when: #out should show checked 2.
3.Report a missexercise · easy · 10 min · practice plan
Search for an absent value and report it.
Passes when: #out should show absent.
4.Search objectsexercise · easy · 10 min · practice plan
Find the record whose id is 2 and show its label.
Passes when: #out should show label beta.
5.Unsorted is fineexercise · medium · 10 min · practice plan
Report whether a linear scan needs sorted input.
Passes when: #out should show no sorting needed.
6.Fix the loop without a breakbug fix · easy · 8 min
Continuing past the match records the last one instead of the first.
Passes when: #out should show index 1.
7.Fix indexOf compared with zerobug fix · easy · 8 min · practice plan
A match at index zero is falsy, so this reports it as missing.
Passes when: #out should show present.
8.Fix find compared by identitybug fix · medium · 8 min · practice plan
Two object literals with the same fields are different references.
Passes when: #out should show label beta.
9.Fix the counter placed after the checkbug fix · easy · 8 min · practice plan
Incrementing after the break undercounts by one.
Passes when: #out should show checked 2.
10.Fix the binary search on unsorted databug fix · easy · 8 min · practice plan
A linear scan is correct here; binary search would need ordering.
Passes when: #out should show linear scan.
Between them these tasks cover Early exit, Unsorted data and indexOf.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.