← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon SWE online assessment with a tree traversal problem. Pretty standard stuff but the input parsing tripped me up more than the actual algorithm.

Questions Asked (1)

Q1

Given a list of employees (each with an id, importance value, and list of direct subordinate ids), compute the total importance of a target employee by summing their importance with all direct and indirect subordinates.

Algorithms & Data Structures
Author's notes

Classic BFS/DFS problem once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the employee hierarchy as a tree and perform a traversal (DFS or BFS) starting from the target employee, summing importance values as you visit each node. Use a hash map to quickly look up employees by id, and handle potential cycles or invalid ids gracefully.

Pro tip: Mention that you would clarify assumptions about the input (e.g., whether the hierarchy is guaranteed to be a tree, if ids are unique, and if there are cycles) before coding, and discuss trade-offs between recursive and iterative approaches to avoid stack overflow for deep hierarchies.

1. Clarify the problem and constraints

Ask about input size, whether the hierarchy is a tree (no cycles), if all subordinate ids are valid, and if the target employee exists. Confirm the expected output type.

2. Choose data structures

Use a hash map to map employee id to employee object for O(1) lookup. Use a stack (iterative DFS) or queue (BFS) to traverse subordinates, or recursion if depth is manageable.

3. Design the traversal algorithm

Start from the target employee, add their importance to a running total, then push all direct subordinates onto the stack/queue. Continue until no more subordinates, summing importance along the way.

4. Handle edge cases

Consider cases where the target employee has no subordinates, the id is invalid, or there are cycles (if not guaranteed a tree). Use a visited set to avoid infinite loops if cycles are possible.

5. Analyze complexity and test

Time complexity is O(N) where N is number of employees in the subtree, space O(N) for the map and traversal. Walk through a small example to verify correctness.

Key Points to Mention

  • Use of hash map for O(1) employee lookup by id
  • Tree traversal (DFS or BFS) to visit all subordinates
  • Handling of edge cases: invalid id, no subordinates, cycles
  • Time and space complexity analysis
  • Trade-offs between recursive and iterative approaches
  • Importance of clarifying assumptions before coding

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