← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Google SWE coding round, one problem the whole time: compute total file system size from a root node. Seemed straightforward but the follow-ups were where things got interesting.

Questions Asked (3)

Q1

Given a file system represented as a tree, write a function that computes the total size of all files reachable from a given root directory node.

Algorithms & Data Structures
Author's notes

Pretty clean recursive problem once you accept it's just a tree traversal.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the node structure (file vs directory, size attribute) and then present a recursive DFS solution that sums file sizes and recurses into subdirectories. Discuss iterative alternatives and edge cases like empty directories or cycles.

Pro tip: Mention that this is essentially a tree traversal problem and that you'd use post-order traversal to accumulate sizes; also note that for very deep trees, an iterative stack avoids recursion limits.

1. Clarify the problem

Ask about the node structure: does each node have a size attribute? How are files and directories distinguished? Are there symlinks or cycles?

2. Choose an approach

Decide between recursive DFS, iterative DFS, or BFS. Recursive DFS is simplest and mirrors the tree structure.

3. Define the algorithm

For a given node: if it's a file, return its size; if it's a directory, sum the results of recursively calling the function on each child.

4. Analyze complexity

Time complexity is O(n) where n is the number of nodes; space complexity is O(h) for recursion stack, where h is the tree height.

5. Handle edge cases

Consider empty directories (return 0), null root (return 0 or throw), and potential cycles (use a visited set if needed).

Key Points to Mention

  • Tree traversal (DFS/BFS) and post-order accumulation
  • Recursive vs iterative implementation trade-offs
  • Time and space complexity analysis
  • Edge cases: empty directories, null root, cycles
  • Modularity: separate file and directory handling
  • Potential for memoization if sizes are queried repeatedly

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

Q2

What are the time and space complexities of your solution?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Said O(N) time without hesitation, which is right.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

State the time and space complexity of your solution clearly, using Big-O notation, and explain how you derived them from your code. Relate the complexities to the input size and any auxiliary data structures used, and briefly discuss trade-offs if applicable.

Pro tip: Always mention the worst-case complexity and clarify if average-case differs; also, if you optimized space at the cost of time or vice versa, explain your reasoning—this shows you consider practical constraints.

1. Identify the input size variable

Define what N represents (e.g., number of elements, length of string) and any other relevant variables like M for a second input.

2. Analyze time complexity

Break down your algorithm into loops, recursion, or operations, and count how many times each executes relative to N. Express the total as a Big-O term, ignoring constants and lower-order terms.

3. Analyze space complexity

Consider all memory used: input storage (if modified), auxiliary data structures (arrays, hash maps, recursion stack), and output. Sum them and express as Big-O, again ignoring constants.

4. Explain derivation and trade-offs

Briefly justify why the complexities are what they are, and if you made any trade-offs (e.g., using extra space to reduce time), mention them.

5. State final answer clearly

Conclude with a concise statement: 'The time complexity is O(...) and space complexity is O(...).'

Key Points to Mention

  • Big-O notation and its meaning (upper bound)
  • Worst-case vs. average-case complexity
  • How each part of the code contributes to time complexity (e.g., nested loops, recursion depth)
  • Auxiliary space vs. total space (including input/output)
  • Trade-offs between time and space (e.g., memoization, in-place algorithms)
  • Any assumptions made about input size or constraints

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

Q3

If the file system allowed shared references where the same subtree could be reached from multiple parents, how would you use memoization to avoid recomputing subtree sizes?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one I actually liked.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Treat the file system as a directed acyclic graph (DAG) where nodes can have multiple parents, and use memoization to cache subtree sizes for each node. When computing a node's subtree size, recursively compute and cache sizes of its children, reusing cached values to avoid redundant work. Discuss how to handle cycles if they are possible, and analyze time and space complexity.

Pro tip: Mention that memoization turns exponential recomputation into linear time relative to the number of unique nodes, but be prepared to discuss trade-offs like increased memory usage and the need for cycle detection if the graph isn't guaranteed acyclic.

1. Model as a DAG

Recognize that shared references create a directed acyclic graph (DAG) where nodes can have multiple parents. Clarify that if cycles are possible, you must detect and handle them to avoid infinite recursion.

2. Define memoization key

Choose a unique identifier for each node (e.g., inode number or file path) to use as the key in a hash map that stores computed subtree sizes.

3. Recursive computation with caching

Implement a recursive function that checks the cache first; if the size is not cached, compute it by summing the sizes of all children (recursively) plus one for the node itself, then store the result in the cache.

4. Handle cycles and shared references

If cycles are possible, use a visited set during recursion to detect cycles and either break them or report an error. For shared references, ensure that each node is computed only once, leveraging the cache across different parents.

5. Analyze complexity and trade-offs

Explain that with memoization, each node is visited once, giving O(N) time and O(N) space for the cache. Discuss trade-offs: memory overhead for caching, potential for stack overflow with deep recursion, and the need for cycle detection if the graph isn't a DAG.

Key Points to Mention

  • Directed acyclic graph (DAG) representation of file system with shared subtrees
  • Memoization using a hash map keyed by unique node identifiers (e.g., inode numbers)
  • Recursive depth-first traversal with caching to avoid recomputation
  • Cycle detection using a visited set if the graph may contain cycles
  • Time complexity reduction from exponential to O(N) where N is number of unique nodes
  • Space complexity O(N) for the cache and potential stack depth issues

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