← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Junior

JuniorPrefer not to say
Apr 2026

Summary

Google SWE onsite coding round, early career level. The main problem was a file system traversal question with a few follow-ups that got progressively trickier. Not a bad experience but the follow-ups definitely pushed past what I'd prepared for.

Questions Asked (4)

Q1

Given the root node of a file system where each node has a class, size, and contains field, compute the total size of all files in the tree.

Algorithms & Data Structures
Author's notes

The base case is pretty clean, just a DFS summing sizes at file nodes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the tree structure and node semantics, then propose a recursive depth-first traversal that sums file sizes and ignores directories. Discuss iterative alternatives and complexity analysis to show thoroughness.

Pro tip: Mention that directories should not contribute to the total size and that you would confirm this assumption with the interviewer. Also, highlight that recursion depth could be an issue for very deep trees, and suggest an iterative approach as a fallback.

1. Clarify the problem

Ask questions to confirm the node structure, what 'class' represents, and whether directories have sizes that should be excluded. Ensure you understand the expected output.

2. Choose a traversal strategy

Decide between recursive DFS, iterative DFS, or BFS. For this problem, DFS is natural because you need to aggregate sizes from all descendants.

3. Design the algorithm

Write a function that traverses the tree, adding the size of each file node to a running total. For directories, recursively process their children.

4. Analyze complexity

State that the time complexity is O(n) where n is the number of nodes, and space complexity is O(h) for recursion stack, where h is the tree height.

5. Discuss edge cases and optimizations

Consider empty tree, nodes with no children, very deep trees (stack overflow), and potential for parallel processing if the tree is large.

Key Points to Mention

  • Tree traversal (DFS/BFS) and recursion
  • Node structure: class, size, contains
  • Distinguishing files from directories
  • Time and space complexity analysis
  • Handling edge cases (empty tree, deep recursion)
  • Iterative vs recursive trade-offs

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

Q2

What is the time complexity of your traversal, and how would you speed up repeated size queries on the same directory?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I said O(N) for the DFS which was fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clearly stating the time complexity of your traversal, including best, average, and worst cases, and the factors that influence it. Then, discuss strategies to optimize repeated size queries, such as caching results, using memoization, or maintaining auxiliary data structures. Finally, analyze the trade-offs between time and space complexity for each approach.

Pro tip: Demonstrate awareness of real-world constraints: mention that caching may become stale if the directory changes, and propose invalidation strategies or incremental updates. This shows you consider maintainability and correctness, not just raw performance.

1. State traversal complexity

Clearly specify the time complexity of your traversal algorithm (e.g., O(n) for n files/subdirectories) and explain what n represents. Mention if it's depth-first or breadth-first and any overhead.

2. Identify repeated query problem

Acknowledge that repeated size queries on the same directory without optimization lead to redundant work, resulting in O(n) per query and O(k*n) for k queries.

3. Propose optimization techniques

Suggest caching the computed size (e.g., in a hash map keyed by directory path) or maintaining a tree structure with subtree sizes. For dynamic directories, consider incremental updates or invalidation on changes.

4. Analyze trade-offs

Compare time vs. space: caching reduces query time to O(1) but uses extra memory and may require invalidation logic. Discuss scenarios where each approach is preferable.

5. Conclude with recommendation

Summarize the best approach based on assumptions (e.g., static vs. dynamic directories) and mention potential edge cases like symbolic links or permission issues.

Key Points to Mention

  • Time complexity of traversal: O(n) where n is total number of files and subdirectories, assuming each node is visited once.
  • Space complexity of traversal: O(d) for recursion depth or O(n) for iterative with explicit stack/queue.
  • Caching/memoization: store directory sizes in a map to achieve O(1) subsequent queries.
  • Trade-offs: memory overhead, cache invalidation, and staleness for dynamic directories.
  • Alternative: maintain a tree with subtree sizes, updated incrementally on file changes.
  • Edge cases: symbolic links, hard links, permissions, and concurrent modifications.

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

Q3

If a file is added or removed from the file system, how do you efficiently invalidate the cache?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Didn't see this coming at all.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the cache architecture (e.g., in-memory, distributed, or OS-level) and the consistency requirements. Then propose a mechanism like inotify or a versioning scheme to detect changes and invalidate only affected entries, discussing trade-offs between precision and overhead.

Pro tip: Mention that invalidation should be idempotent and consider using a generation number or epoch to avoid race conditions, showing awareness of concurrency issues in real systems.

1. Clarify the scenario

Ask about the cache type, scale, and consistency model (e.g., strong vs. eventual) to tailor your answer.

2. Choose a detection mechanism

Propose using OS-level file system events (inotify, FSEvents, kqueue) or a versioning scheme to detect additions/removals.

3. Design invalidation strategy

Decide between invalidating specific entries (e.g., by path) or using a coarse-grained approach like a global version bump, balancing precision and overhead.

4. Handle concurrency and consistency

Ensure invalidation is atomic and idempotent, and consider using locks or epochs to prevent stale reads during updates.

5. Discuss trade-offs and optimizations

Compare approaches (e.g., event-driven vs. polling) in terms of latency, scalability, and complexity, and mention possible optimizations like batching.

Key Points to Mention

  • Use of file system event monitoring APIs (inotify, FSEvents, kqueue) for efficient change detection.
  • Versioning or generation numbers to invalidate cache entries without scanning the entire cache.
  • Trade-offs between fine-grained (per-file) and coarse-grained (global) invalidation.
  • Concurrency control: ensuring invalidation is atomic and idempotent to avoid race conditions.
  • Handling of directory-level changes (e.g., file added to a directory) and recursive invalidation.
  • Scalability considerations for distributed caches (e.g., using a message bus to propagate invalidation events).

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

Q4

Can you rewrite your recursive DFS solution using an explicit stack instead?

Algorithms & Data Structures
Author's notes

Standard ask but I hadn't drilled it recently enough.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, explain the general transformation from recursion to iteration using an explicit stack, then walk through the specific changes needed for DFS. Emphasize that the stack simulates the call stack, and discuss how to handle state such as visited nodes and processing order.

Pro tip: Mention that the explicit stack approach can be more memory-efficient and avoids recursion depth limits, but be prepared to discuss trade-offs like code complexity. Also, clarify whether the problem requires pre-order, in-order, or post-order traversal, as the stack implementation differs.

1. Understand the recursive solution

Briefly restate the recursive DFS logic, including base cases, recursive calls, and any state maintained (e.g., visited set, path).

2. Identify the call stack behavior

Explain how the recursion uses the call stack to remember nodes to visit, and what information each stack frame holds (e.g., current node, next neighbor to process).

3. Design the explicit stack

Choose what to push onto the stack: nodes alone or nodes with additional state (like an iterator or a flag for post-order). Describe the loop condition and how to process each popped element.

4. Handle visited and ordering

Detail how to avoid revisiting nodes (e.g., mark visited when pushing or popping) and ensure the traversal order matches the recursive version.

5. Walk through an example

Trace the iterative code on a small graph or tree to demonstrate correctness and highlight any edge cases (e.g., disconnected graphs, cycles).

Key Points to Mention

  • The explicit stack replaces the call stack, so it should store the same information as recursive calls.
  • For DFS, the stack is LIFO, so push neighbors in reverse order to process them in the original order.
  • Visited marking can be done when pushing to the stack to avoid duplicates, but be careful with post-order traversal.
  • Post-order traversal requires a two-phase approach: push node with a flag or use two stacks.
  • The iterative version avoids recursion depth limits and can be more memory-efficient for deep graphs.
  • Time and space complexity remain O(V+E) and O(V) respectively, but constant factors may differ.

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