KatInterview

Reconcile User Settings from Snapshot and Activity Log

SQL & DataAuto-graded · 17 test cases
EasyAuto-gradedSQL & Datadata-manipulationjoinsstate-reconciliation

Last 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] where setting is either "on" or "off"

changes — all setting modifications made during February 2020:

  • Each entry is [user_id, change_date, action]
  • change_date is a string in "YYYY-MM-DD" format between "2020-02-01" and "2020-02-29"
  • action is 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 changes log 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_setting is "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 changes have their setting determined by their last action

Constraints

  • 0 ≤ len(baseline) ≤ 10^5
  • 0 ≤ len(changes) ≤ 10^6
  • All user_id values 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.