Write SQL to analyze CA residency history
RobinhoodData Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to write SQL for temporal state reconstruction, including correct joins on composite keys, interpreting edit/audit logs, and reasoning about inclusive/exclusive timestamps.
You have two SQL tables.
Table 1: user_info (current snapshot)
id(user id)shard(partition column)namecountrystatecreated_date
Table 2: user_info_edit_log (edit history)
idshardcreated_date(when the change happened)table_namefield_nameold_valuenew_value
Assumptions/constraints:
- When joining the tables, you must join on both
(id, shard). - For this question, you can assume
table_name = 'user_info'andfield_name = 'state'are the only relevant values (i.e., only state changes foruser_info). - Output columns for all questions:
(id, shard, name). - Interpret "as of 2021-01-01" as the user’s state at timestamp
2021-01-01 00:00:00(inclusive of changes at that instant).
Write SQL for each of the following:
- Who always lived in CA as of 2021-01-01?
- Meaning: their state on
2021-01-01isCA, and there is no evidence they were ever in a non-CA state before that time.
- Who moved to CA before 2021-01-01 and lived in CA as of 2021-01-01?
- Meaning: their state on
2021-01-01isCA, and they changed from a non-CA state toCAat some time strictly before2021-01-01.
- Who might have moved out of CA after 2021-01-01?
- Meaning: they were in
CAas of2021-01-01, and there is a state-change record after2021-01-01indicatingCA -> (not CA).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.