← cortex Interview Insights

cortex·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Got a coding question at Cortex that was basically a tree traversal problem dressed up as an HR tool. Pretty straightforward if you've done BFS before, but the CSV parsing part tripped me up for a minute.

Questions Asked (1)

Q1

Given a CSV file with employee name, title, department, and manager name columns, build the company's org hierarchy and print employees level by level: the root (CEO or whoever has no manager) on line one, their direct reports on line two, and so on down the tree using BFS.

Algorithms & Data StructuresData Modeling
Author's notes

I knew it was BFS the second they said 'level by level' but I fumbled the setup.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Parse the CSV into a list of employee records, then build an adjacency list mapping each manager to their direct reports. Identify the root (employee with no manager) and perform a BFS level by level, printing each level on a separate line. Handle edge cases like multiple roots, cycles, or missing managers.

Pro tip: Clarify with the interviewer whether the hierarchy is guaranteed to be a tree (single root, no cycles) or if it could be a forest or contain cycles. This shows you think about data integrity and robustness, and you can then discuss how to handle those cases.

1. Parse and Validate Input

Read the CSV file, extract the relevant columns (name, manager), and validate that each employee has a unique name and that manager references are valid. Handle missing or empty manager fields as potential roots.

2. Build the Graph

Create an adjacency list (dictionary) where each key is a manager's name and the value is a list of their direct reports. Also, collect all employees and identify the root(s) as those who do not appear as a manager or have no manager.

3. Perform BFS Level by Level

Use a queue to traverse the tree starting from the root. For each level, process all nodes currently in the queue, collect their children for the next level, and print the current level's employees. Repeat until the queue is empty.

4. Handle Edge Cases and Output

Address scenarios like multiple roots (forest), cycles (if data is malformed), or employees with missing managers. Ensure the output format matches the requirement: each level on a new line, employees separated by spaces or commas.

Key Points to Mention

  • Time and space complexity: O(N) time and O(N) space, where N is the number of employees.
  • Choice of data structures: adjacency list (hash map) for efficient lookup of direct reports, and a queue for BFS.
  • Handling of multiple roots or disconnected components (forest) and how to output them.
  • Cycle detection to avoid infinite loops if the data is not a valid tree.
  • Memory considerations for large CSV files (e.g., streaming vs. loading all into memory).
  • Output formatting: ensuring each level is printed on a separate line and employees are clearly separated.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.