Flatten nested incident records, map severity to priority, assign IDs and sort

Quick Overview

Flatten nested dictionary records into dot-notation keys, convert a severity label into a numeric priority, assign zero-padded incident IDs by input position, and return the records sorted by priority and ID. The problem tests careful data transformation, iterative traversal of nested structures, and multi-key sorting with a unique tie-break.

Flatten nested incident records, map severity to priority, assign IDs and sort

Company: ServiceNow

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a list of raw records where each record is a dictionary with potentially nested fields, process each record into a structured format suitable for systems like ServiceNow Incidents or Change Requests. For each record, perform the following transformations: - Flatten nested dictionaries using dot notation for keys (for example, `{"a": {"b": "c"}}` becomes `{"a.b": "c"}`). - Extract the `severity` field and map it to a numeric `priority`: `"critical"` is `1`, `"high"` is `2`, `"medium"` is `3` and `"low"` is `4`. - Generate a `record_id` in the format `"INC"` followed by a zero-padded 7-digit index starting from `1` (for example, `"INC0000001"`). Return all structured records sorted by `priority` ascending, then by `record_id` ascending. Each output record should contain `record_id`, `priority`, and all flattened fields from the original record, with `severity` replaced by `priority`. ### Function Signature ```python def process_records(records: list[dict]) -> list[dict]: ``` ### Rules - The index in `record_id` is the record's 1-based position in the input list, assigned before sorting. The first input record is always `"INC0000001"`, wherever it lands in the output. - A flattened key joins every key on the path from the top-level dictionary down to the leaf with `.`. A nested dictionary with no keys contributes no fields. - Only the top-level `severity` key is replaced by `priority`. A nested key with the same name (for example `details.severity` after flattening) is an ordinary field and is copied unchanged. - Build each output record with `record_id` first, then `priority`, then the flattened fields in ascending key order. Python dictionaries compare equal regardless of key order, so this only affects how the result prints. ### Constraints - `1 <= len(records) <= 1000` - Each record is a dictionary with string keys. - Nesting depth does not exceed `5` levels. - Every record has a top-level `severity` field, and its value is one of `"critical"`, `"high"`, `"medium"`, `"low"`. - All leaf values are strings. - No key contains a `.`, and no record has a top-level key named `record_id` or `priority`, so flattened keys never collide. ### Examples ```text Input: records = [ {"title": "Server Down", "severity": "critical", "details": {"location": "US-East", "team": "Infra"}}, {"title": "Slow Response", "severity": "medium", "details": {"location": "EU-West", "team": "App"}}, {"title": "Disk Full", "severity": "high", "details": {"location": "US-West", "team": "Storage"}} ] Output: [ {"record_id": "INC0000001", "priority": 1, "details.location": "US-East", "details.team": "Infra", "title": "Server Down"}, {"record_id": "INC0000003", "priority": 2, "details.location": "US-West", "details.team": "Storage", "title": "Disk Full"}, {"record_id": "INC0000002", "priority": 3, "details.location": "EU-West", "details.team": "App", "title": "Slow Response"} ] ``` The records get IDs INC0000001, INC0000002 and INC0000003 in input order. Sorting by priority puts the critical record (1) first, then the high one (2), then the medium one (3), so INC0000003 comes before INC0000002. ```text Input: records = [ {"title": "Minor Bug", "severity": "low", "info": {"reporter": "Alice", "context": {"module": "UI"}}} ] Output: [ {"record_id": "INC0000001", "priority": 4, "info.context.module": "UI", "info.reporter": "Alice", "title": "Minor Bug"} ] ``` The single record is nested two levels deep, so `module` becomes `info.context.module` and `reporter` becomes `info.reporter`. Its severity is low, which maps to priority 4. ```text Input: records = [ {"title": "Login Errors", "severity": "high"}, {"title": "Payment Outage", "severity": "critical", "owner": {"team": "Billing"}}, {"title": "Cache Misses", "severity": "high"} ] Output: [ {"record_id": "INC0000002", "priority": 1, "owner.team": "Billing", "title": "Payment Outage"}, {"record_id": "INC0000001", "priority": 2, "title": "Login Errors"}, {"record_id": "INC0000003", "priority": 2, "title": "Cache Misses"} ] ``` The critical record sorts first. The two high records tie on priority 2, so `record_id` breaks the tie and INC0000001 comes before INC0000003.

Overview: Flatten nested dictionary records into dot-notation keys, convert a severity label into a numeric priority, assign zero-padded incident IDs by input position, and return the records sorted by priority and ID. The problem tests careful data transformation, iterative traversal of nested structures, and multi-key sorting with a unique tie-break.

ServiceNow logo
ServiceNow
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a list of raw records where each record is a dictionary with potentially nested fields, process each record into a structured format suitable for systems like ServiceNow Incidents or Change Requests.

For each record, perform the following transformations:

  • Flatten nested dictionaries using dot notation for keys (for example, {"a": {"b": "c"}} becomes {"a.b": "c"} ).
  • Extract the severity field and map it to a numeric priority : "critical" is 1 , "high" is 2 , "medium" is 3 and "low" is 4 .
  • Generate a record_id in the format "INC" followed by a zero-padded 7-digit index starting from 1 (for example, "INC0000001" ).

Return all structured records sorted by priority ascending, then by record_id ascending.

Each output record should contain record_id, priority, and all flattened fields from the original record, with severity replaced by priority.

Function Signature

def process_records(records: list[dict]) -> list[dict]:

Rules

  • The index in record_id is the record's 1-based position in the input list, assigned before sorting. The first input record is always "INC0000001" , wherever it lands in the output.
  • A flattened key joins every key on the path from the top-level dictionary down to the leaf with . . A nested dictionary with no keys contributes no fields.
  • Only the top-level severity key is replaced by priority . A nested key with the same name (for example details.severity after flattening) is an ordinary field and is copied unchanged.
  • Build each output record with record_id first, then priority , then the flattened fields in ascending key order. Python dictionaries compare equal regardless of key order, so this only affects how the result prints.

Constraints

  • 1 <= len(records) <= 1000
  • Each record is a dictionary with string keys.
  • Nesting depth does not exceed 5 levels.
  • Every record has a top-level severity field, and its value is one of "critical" , "high" , "medium" , "low" .
  • All leaf values are strings.
  • No key contains a . , and no record has a top-level key named record_id or priority , so flattened keys never collide.

Examples

Input: records = [
  {"title": "Server Down", "severity": "critical", "details": {"location": "US-East", "team": "Infra"}},
  {"title": "Slow Response", "severity": "medium", "details": {"location": "EU-West", "team": "App"}},
  {"title": "Disk Full", "severity": "high", "details": {"location": "US-West", "team": "Storage"}}
]
Output: [
  {"record_id": "INC0000001", "priority": 1, "details.location": "US-East", "details.team": "Infra", "title": "Server Down"},
  {"record_id": "INC0000003", "priority": 2, "details.location": "US-West", "details.team": "Storage", "title": "Disk Full"},
  {"record_id": "INC0000002", "priority": 3, "details.location": "EU-West", "details.team": "App", "title": "Slow Response"}
]

The records get IDs INC0000001, INC0000002 and INC0000003 in input order. Sorting by priority puts the critical record (1) first, then the high one (2), then the medium one (3), so INC0000003 comes before INC0000002.

Input: records = [
  {"title": "Minor Bug", "severity": "low", "info": {"reporter": "Alice", "context": {"module": "UI"}}}
]
Output: [
  {"record_id": "INC0000001", "priority": 4, "info.context.module": "UI", "info.reporter": "Alice", "title": "Minor Bug"}
]

The single record is nested two levels deep, so module becomes info.context.module and reporter becomes info.reporter. Its severity is low, which maps to priority 4.

Input: records = [
  {"title": "Login Errors", "severity": "high"},
  {"title": "Payment Outage", "severity": "critical", "owner": {"team": "Billing"}},
  {"title": "Cache Misses", "severity": "high"}
]
Output: [
  {"record_id": "INC0000002", "priority": 1, "owner.team": "Billing", "title": "Payment Outage"},
  {"record_id": "INC0000001", "priority": 2, "title": "Login Errors"},
  {"record_id": "INC0000003", "priority": 2, "title": "Cache Misses"}
]

The critical record sorts first. The two high records tie on priority 2, so record_id breaks the tie and INC0000001 comes before INC0000003.