← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Amazon SWE coding round, one question, stack-based simulation. Nothing fancy but it took me longer than I'd like to admit.

Questions Asked (1)

Q1

Given a list of logs from a single-threaded CPU, calculate the exclusive execution time for each function.

Algorithms & Data Structures
Author's notes

Stack simulation problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to track the currently executing function and maintain a running total of exclusive time. When a function starts, push its ID and start time; when it ends, compute its exclusive time by subtracting nested calls, then add that time to the parent's exclusive time if a parent exists.

Pro tip: Clarify the log format and edge cases upfront—like whether timestamps are inclusive/exclusive and if nested calls can span multiple logs—to avoid off-by-one errors and show attention to detail.

1. Clarify assumptions and log format

Confirm the structure of each log entry (e.g., 'function_id:start:timestamp' or 'function_id:end:timestamp') and whether timestamps are integers representing time units. Ask about edge cases like multiple functions with the same ID or non-nested sequential calls.

2. Initialize data structures

Use a stack to store pairs of (function_id, start_time) for currently executing functions. Use a hash map or array to accumulate exclusive time per function ID.

3. Process logs sequentially

For each log: if it's a start, push the function ID and timestamp onto the stack. If it's an end, pop the top, compute exclusive time as (end_time - start_time + 1) minus any time spent in nested calls (tracked via a variable or by adjusting parent's start time), and add to the function's total.

4. Handle nested calls and parent updates

When a function ends, if the stack is not empty, the parent's exclusive time should be reduced by the child's total execution time (including nested calls). Alternatively, update the parent's start time to the child's end time + 1 to account for the child's duration.

5. Return results and verify with examples

After processing all logs, return the exclusive times for each function. Walk through a simple example (e.g., single function, nested functions) to verify correctness and discuss time/space complexity.

Key Points to Mention

  • Stack-based approach to track function call hierarchy
  • Handling of nested function calls and exclusive time calculation
  • Time complexity O(n) and space complexity O(n) where n is number of logs
  • Edge cases: single function, deeply nested calls, sequential calls, and multiple functions with same ID
  • Timestamp inclusivity/exclusivity and off-by-one errors
  • Use of hash map to store results for quick lookup

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