Syllabus Lesson 35 of 141 · Ask a different question
Ask a different question

Invert it: who never did it?

Why it matters "Which customers never..." is one of the most common questions a product team asks, and its literal translation into code is a loop inside a loop over the biggest table you have.

Security wants to email every user who has not turned on two-factor sign-in. You have the list of current users and the activity log for the quarter. The log lives in a metered archive: every row you read is billed, and the job runs on a small worker with a step budget.

The data

users = ["u1", "u2", "u3"]
log = Tape([("u1", "login"), ("u2", "enable_2fa"), ("old9", "enable_2fa")])
never_did(users, log, "enable_2fa")   # -> ["u1", "u3"]

Notice old9: the log still has rows for people who have left. They are not users any more, so they never appear in the answer. And u3 has no rows at all, so they are in the answer.

Your turn

Write never_did(users, log, action). users is a plain list of unique user ids. log is a Tape of (user_id, action) rows, in no useful order; it may mention ids that are not in users. Return the sorted list of ids from users that have no row with this action. Budget: with 2,000 users and 40,000 log rows you may read at most 80,000 rows (two passes) and run at most 500,000 steps of your own code.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.