← Robinhood Interview Insights
Three separate conditions and I kept collapsing two of them together in my head.
Use a CTE to find the earliest state-change edit log entry for each user (id, shard) and filter for entries where the new state is 'CA' and the change timestamp is before 2021-01-01. Then join this back to the users table to ensure the user's initial state (from the users table) was not 'CA' and their current state as of that date is still 'CA'. Finally, select id, shard, and name.
Pro tip: Clarify the schema: the users table likely has a 'state' column representing the current state, and the edit log has 'old_state' and 'new_state' columns. Also, consider that a user might have multiple state changes; you need the first change to CA before the cutoff and no subsequent changes away from CA before the cutoff.
Identify the relevant columns: users table (id, shard, name, state), edit log table (id, shard, timestamp, old_state, new_state). Clarify that 'started with a state other than CA' means the user's initial state (before any edits) was not CA, and 'current state is still CA as of that date' means as of 2021-01-01, the user's state was CA.
For each user (id, shard), find the earliest edit log entry where new_state = 'CA' and timestamp < '2021-01-01'. Ensure that this is indeed the first change to CA (i.e., no earlier change to CA).
Check that there are no edit log entries after the first change to CA and before 2021-01-01 where new_state != 'CA'. This ensures the user's state remained CA as of the cutoff date.
Join the filtered edit log entries with the users table on id and shard. Filter for users whose initial state (from users table) is not 'CA'. Also, ensure the users table's current state is 'CA' (or rely on the edit log to confirm).
Return id, shard, and name for the qualifying users.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.