KatInterview

Iterative Nested List Flattening and UTC Active User Metrics

SQL & DataModel answer
MediumModel answerSQL & Datapythonsqlstacks-queues

Last updated

Implement an iterative nested list flattener in Python with edge-case handling, and construct a SQL query to compute Daily Active Users (DAU) using strict UTC date casting.

You are asked to solve two distinct data processing tasks common in software engineering and data platform engineering interviews.


Task 1: Python Iterative List Flattening

Write a function flatten(nested) in Python that accepts a input containing integers or deeply nested lists of integers, returning a flattened list of integers preserving left-to-right element order.

Requirements

  1. Iterative Design: Avoid reliance on recursive function calls to prevent stack overflow errors when encountering heavily nested inputs (e.g., nesting depth $\ge 10^4$).
  2. Type Enforcement: Only integers (excluding booleans) and lists are valid data elements. Raise a TypeError if non-list types are passed at the top level, or if any invalid types (such as floats, strings, None, dictionaries, or boolean values) are encountered during traversal.
  3. Complexity & Testing:
    • Define the Big-O time and space complexity of your implementation.
    • Provide explicit test coverage demonstrating correctness for empty lists, extreme nesting depths, invalid types, and cyclic list references.

Task 2: SQL Daily Active Users (DAU) Calculation

Given a database table named events with the following schema:

Column NameData TypeDescription
user_idINTUnique identifier for the user
event_timeTIMESTAMP or TIMESTAMPTZTimestamp when the action occurred
event_typeVARCHARDescription of the action (e.g., 'click', 'login')

Write an ANSI SQL compliant query that calculates the Daily Active Users (DAU) for each UTC calendar date.

Requirements

  1. UTC Uniformity: Ensure timestamps are explicitly evaluated in UTC, guarding against unexpected session time-zone shifts.
  2. Deduplication: A user who triggers multiple events within the same UTC calendar day must only be counted once for that date.
  3. Output: Return two columns named event_date and dau, ordered chronologically by event_date.

Hints

Hint 1

For the Python flattening task, maintaining an explicit stack of iterators (using `iter()`) allows depth-first traversal without copying list items onto the stack ahead of time.

Hint 2

Remember that in Python, `isinstance(True, int)` evaluates to `True`. You must explicitly disallow `bool` instances if only raw integers are allowed.

Hint 3

In SQL, casting a timezone-aware timestamp directly to a `DATE` can yield different results depending on the database session time zone. Use explicit `AT TIME ZONE 'UTC'` clauses.