KatInterview

Sequence Numbering Within Grouped Records

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datawindow-functionssortinggrouping

Last updated

Tests ability to assign sequential positions to records within partitions using sorting and grouping logic.

You are given a list of entry records. Each entry belongs to a conversation identified by context_uuid. Your task is to assign each entry a sequential position number within its conversation.

Each entry is represented as a dictionary with these fields:

  • entry_uuid: unique identifier for the entry
  • context_uuid: identifier for the conversation containing this entry
  • visitor_id: identifier for the visitor who created the entry
  • created_at: timestamp when the entry was created
  • platform: platform where the entry was created

Numbering Rules:

  1. Position numbers start at 1 within each distinct context_uuid
  2. Within a conversation, entries are ordered by created_at in ascending order
  3. If two entries have identical created_at values, order them by entry_uuid in ascending order
  4. Null values in sort keys should be treated as smaller than any non-null value (appear first)

Return a new list where each entry dictionary includes an additional entry_number field containing its position. The fields in each output dictionary must appear in this exact order: entry_uuid, context_uuid, visitor_id, created_at, platform, entry_number.

The output list may be in any order.

Constraints:

  • 0 ≤ number of entries ≤ 10,000
  • entry_uuid, context_uuid, visitor_id, created_at, and platform may be None
  • Timestamps are ISO 8601 strings when present
  • All fields are either strings or None

Examples

Example 1

Input: assign_entry_positions([{'entry_uuid': 'e1', 'context_uuid': 'c1', 'visitor_id': 'v1', 'created_at': '2024-01-01T10:00:00', 'platform': 'web'}, {'entry_uuid': 'e2', 'context_uuid': 'c1', 'visitor_id': 'v1', 'created_at': '2024-01-01T10:01:00', 'platform': 'mobile'}, {'entry_uuid': 'e3', 'context_uuid': 'c2', 'visitor_id': 'v2', 'created_at': '2024-01-01T09:00:00', 'platform': 'web'}])

Output: [{'entry_uuid': 'e1', 'context_uuid': 'c1', 'visitor_id': 'v1', 'created_at': '2024-01-01T10:00:00', 'platform': 'web', 'entry_number': 1}, {'entry_uuid': 'e2', 'context_uuid': 'c1', 'visitor_id': 'v1', 'created_at': '2024-01-01T10:01:00', 'platform': 'mobile', 'entry_number': 2}, {'entry_uuid': 'e3', 'context_uuid': 'c2', 'visitor_id': 'v2', 'created_at': '2024-01-01T09:00:00', 'platform': 'web', 'entry_number': 1}]

Example 2

Input: assign_entry_positions([{'entry_uuid': 'b', 'context_uuid': 'c1', 'visitor_id': 'v1', 'created_at': '2024-01-01T10:00:00', 'platform': 'web'}, {'entry_uuid': 'a', 'context_uuid': 'c1', 'visitor_id': 'v2', 'created_at': '2024-01-01T10:00:00', 'platform': 'mobile'}])

Output: [{'entry_uuid': 'a', 'context_uuid': 'c1', 'visitor_id': 'v2', 'created_at': '2024-01-01T10:00:00', 'platform': 'mobile', 'entry_number': 1}, {'entry_uuid': 'b', 'context_uuid': 'c1', 'visitor_id': 'v1', 'created_at': '2024-01-01T10:00:00', 'platform': 'web', 'entry_number': 2}]

Hints

Hint 1

Think about how you would group entries by conversation and then order them within each group.

Hint 2

Sorting by multiple keys where nulls come first requires careful handling of None values in Python.

Hint 3

A single pass through sorted data with a counter per group is sufficient - no need for nested loops.