Keep Folder Size and File Count Rollups Correct Under Moves

Last updated: November 29, 2026

Quick Overview

Build a structure over a folder tree that returns total bytes and file count under any folder, and stays correct as files are added, items are deleted, and folders are moved, while rejecting moves into a descendant.

Box
Coding & Algorithms
Software Engineer
Box
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Build a structure over a folder tree that returns total bytes and file count under any folder, and stays correct as files are added, items are deleted, and folders are moved, while rejecting moves into a descendant.

Box shows folder sizes and enforces storage quotas, and its engineering blog describes folder aggregation as a large-scale data problem. Onsite this checks whether you can maintain derived data incrementally and reason about the cost of moving a big subtree.

What the Interviewer Expects
  • Computes initial rollups by pushing each file's size up its ancestor chain
  • Moves a subtree by subtracting its contribution from old ancestors and adding to new ones in O(depth)
  • Rejects moving a folder into itself or a descendant
  • Separates file contribution from folder aggregate correctly
  • Discusses when to switch to batch recomputation
Key Topics to Cover
Incremental aggregation
Tree invariants
Ancestor walks
Event-driven updates
Consistency
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you do this when the tree is sharded across databases and updates arrive as events?
  • Why might eventually consistent rollups be acceptable for display but not for quota enforcement?
  • How would you handle file version uploads that change a file's size?
  • What do you do when the incremental totals drift from the truth?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Items have a parent (None for root), a size, and an is_folder flag. Queries: stats(folder) returns (bytes, files) for the whole subtree. Updates: add file, delete item, move item. Folder sizes are t...

High-Level Design

Store per-folder totals. A file contributes its size and 1 to every ancestor. The key idea for moves: a folder's subtree contribution is already summarized in its own totals, so moving it is two ances...


Submit Your Answer
Markdown supported

Related Questions