—
Reason about list-operation complexity
Tutor me on https://mlprep.iwase.dev/item/prog-py-list-complexity/. This is a screening item. If window.mlPrepAgent is available, read attempts for item prog-py-list-complexity before tutoring. Inspect my attempt, keep the item ID, and do not reveal the full answer first. After a real attempt, append its record with recorded_by agent, include agent_session_id when available, and read it back.Problem
For the function below on input values, give tight worst-case time and
auxiliary-space bounds. Then state the expected-time bound after replacing
unique with a set and replacing the final loop by return len(values).
def duplicate_free_score(values):
unique = []
for value in values:
if value not in unique:
unique.append(value)
total = 0
for value in unique:
total += values.count(value)
return totalReveal answer or reference solution
Worst-case time is : list membership can scan elements on each
iteration, and up to calls to values.count each scan elements.
Auxiliary space is for unique. A set-based pass has expected
time under ordinary hash-table assumptions, and len(values) removes the
second scan entirely. The returned score is always .
def complexity_answers() -> tuple[str, str, str]:
return "O(n^2)", "O(n)", "O(n) expected"Public tests
complexity_answers()→('O(n^2)', 'O(n)', 'O(n) expected')
Previous attempts
Loading attempts saved in this browser…