Reconcile User Settings from Snapshot and Activity Log
SQL & DataAuto-graded · 17 test casesLast updated
Tests table joins, state reconciliation, and handling baseline data overridden by timestamped events
You are working with user notification preferences stored in two separate datasets. Your task is to determine the final notification setting for each user as of the end of February 2020 by reconciling a baseline snapshot with a log of setting changes.
Input Data
You receive two lists:
baseline — each user's setting as recorded on 2020-01-31:
- Each entry is
[user_id, setting]wheresettingis either"on"or"off"
changes — all setting modifications made during February 2020:
- Each entry is
[user_id, change_date, action] change_dateis a string in"YYYY-MM-DD"format between"2020-02-01"and"2020-02-29"actionis either"turn_on"or"turn_off"
Rules
- Any user may perform at most one action per calendar day
- All changes in the log occurred strictly after the January 31 snapshot
- The
changeslog may contain users not present in the baseline (users who joined after January 31) - When a user has multiple February actions, only their chronologically last action determines their final state
- When a user has no February actions, their baseline setting remains unchanged
Output
Return a list of [user_id, final_setting] for every user appearing in either input, sorted by user_id in ascending order.
final_settingis"on"if the user's most recent action was"turn_on", or"off"if it was"turn_off"- Users with no February actions retain their baseline setting
- Users appearing only in
changeshave their setting determined by their last action
Constraints
0 ≤ len(baseline) ≤ 10^50 ≤ len(changes) ≤ 10^6- All
user_idvalues are positive integers - All dates are valid and within February 2020
- Settings are always
"on"or"off" - Actions are always
"turn_on"or"turn_off"
Examples
Example 1
Input: reconcile_settings([[1, 'on'], [2, 'off'], [3, 'on']], [[1, '2020-02-10', 'turn_off'], [1, '2020-02-20', 'turn_on'], [2, '2020-02-05', 'turn_on'], [4, '2020-02-12', 'turn_off']])
Output: [[1, 'on'], [2, 'on'], [3, 'on'], [4, 'off']]
Example 2
Input: reconcile_settings([[10, 'off']], [])
Output: [[10, 'off']]
Hints
Hint 1
Think of this as baseline values that get overridden by more recent events—which data source should take precedence?
Hint 2
You need to include users from both datasets. A standard left join will silently drop users who appear only in the changes log.
Hint 3
For users with multiple February actions, you only care about the chronologically last one. Find it first, then merge with the baseline.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires reconciling two data sources with different precedence rules: a baseline snapshot and a more recent activity log.
The key insight is that this is a "baseline plus overrides" pattern. The January 31 snapshot provides default values, but any February activity takes priority. When multiple February actions exist for a user, only the chronologically last one matters since each action sets an absolute state rather than a relative change.
Algorithm:
- Load all baseline settings into a dictionary mapping
user_id → setting - Scan through the changes log and keep only the chronologically last action for each user
- Apply each user's last action to override their setting (or add them if they're new)
- Return all users sorted by ID
Critical details:
- Coverage: We must include users from both datasets. Starting with the baseline dictionary and then inserting/updating from the changes log ensures we capture everyone.
- Precedence: Changes always override baseline values. The
most_recentdictionary captures only the latest action per user, and applying it last gives it priority. - Date comparison: ISO date strings like
"2020-02-15"compare correctly lexicographically, so simple>comparison finds the latest date. - New users: Users appearing only in
changesare added tofinalduring the override step.
Why It Works
- Each action sets the notification state absolutely (
turn_on→"on",turn_off→"off"), so intermediate actions are irrelevant once we have the last one - The baseline represents state at
2020-01-31; all changes happened after that date, so any change beats the baseline - The
finaldictionary grows to include the union of both user sets: it starts with baseline users and gains change-log-only users during the override loop
Complexity
Let B = len(baseline), C = len(changes), and U = total distinct users.
- Time: O(B + C + U log U)
- O(B) to build the baseline dictionary
- O(C) to find each user's most recent change
- O(U log U) to sort the output by user ID
- Space: O(U) for the two dictionaries
Code
from typing import List
def reconcile_settings(baseline: List[List], changes: List[List]) -> List[List]:
# Start with the baseline snapshot for all known users
final = {user_id: setting for user_id, setting in baseline}
# Track the most recent change for each user
most_recent = {}
for user_id, change_date, action in changes:
if user_id not in most_recent or change_date > most_recent[user_id][0]:
most_recent[user_id] = (change_date, action)
# Apply the most recent change (overrides baseline and adds new users)
for user_id, (_, action) in most_recent.items():
final[user_id] = "on" if action == "turn_on" else "off"
return [[user_id, final[user_id]] for user_id in sorted(final)]