Re-implement the same pivot/dedup logic in plain Python (no pandas) after the SQL attempt required unioning many categories
Asked in the Second round (coding) stage. Follow-up after the candidate proposed repeated UNION ALL and the interviewer asked how to avoid unbounded UNIONs. Reported follow-up: What if there are many categories, you can't keep UNION ALL-ing forever, can you?
Write a PostgreSQL query and a plain Python implementation that retain the latest record for each entity and category, then produce one wide row per entity. The Python implementation must not use pandas.
entity_id, entity_name, priority_value, channel_value, region_valuerecorded_at, breaking ties with the greatest record_identity_id ascending| Column | Type | Description |
|---|---|---|
| entity_idPK | INT | Unique entity identifier |
| entity_name | VARCHAR(100) | Entity display name |
| Column | Type | Description |
|---|---|---|
| record_idPK | INT | Unique category record identifier |
| entity_id | INT | Referenced entity |
| category | VARCHAR(50) | Category name |
| value | VARCHAR(100) | Category value |
| recorded_at | TIMESTAMP | Time the category value was recorded |