Mark Seen Indices
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Practice workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Practice workspace
Problem
Implement mark_seen_indices(values). values contains integers from 1 through len(values). Return a boolean list of length len(values) whose index i is True exactly when i + 1 appears in values. Practice the value-minus-one index mapping.
Starter code
def mark_seen_indices(values):
passTest cases
duplicates-and-gaps
{
"args": [
[
4,
1,
4,
2
]
]
}Expected: [true,true,false,true]
all-present
{
"args": [
[
3,
1,
2
]
]
}Expected: [true,true,true]
Wizard outline
- Step 1: Allocate one marker per label
Create a False-filled list with the same length as values. The allowed labels are 1 through n, so the output needs exactly n independent slots.
- Step 2: Map one-based values to zero-based indices
Set seen[value - 1] for every input value. Subtracting one aligns label 1 with index 0 and label n with index n - 1.
Footguns and prerequisites
- Using value as the index shifts every mark and fails for the maximum label n.
- Toggling a position instead of assigning True makes duplicate values erase earlier evidence.
- arrays strings two pointers sliding window
Reviewed references
Prepared Interview Problems
- Find All Duplicates in an Array(opens in a new tab)
Mark Seen Indices isolates after reading a value v, result[v - 1] is true and every previously marked position stays true. That focused state discipline is required when implementing find all duplicates array as a complete Interview Problem.
Recommended approach and implementation
A bounded 1..n value domain can map directly into n array positions without a dictionary key lookup. After reading a value v, result[v - 1] is true and every previously marked position stays true.
Why it works: Every valid value maps to one unique zero-based position, so marking that position records membership without losing earlier marks. Reading the completed boolean array therefore reports exactly the values that occurred.
def mark_seen_indices(values):
seen = [False] * len(values)
for value in values:
seen[value - 1] = True
return seen