Measure the Widest Level of a Fulfillment Network Tree

Last updated: November 29, 2026

Quick Overview

A fulfillment network is a binary tree. The width of a level is the number of positions between its leftmost and rightmost existing nodes, counting the gaps where nodes would sit in a complete tree. Return the maximum width across all levels.

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

0

0

0 solved


A fulfillment network is a binary tree. The width of a level is the number of positions between its leftmost and rightmost existing nodes, counting the gaps where nodes would sit in a complete tree. Return the maximum width across all levels.

An onsite coding question in the spirit of Amazon's tree-heavy loops, framed as sizing the display canvas for a network map. The interviewer checks that you index nodes like a heap, that you count the gaps, and that you avoid index overflow on deep skewed trees.

What the Interviewer Expects
  • Assign heap-style positions: left child 2i, right child 2i+1
  • Process level by level and compute last minus first plus one
  • Renormalize indices per level so they stay small
  • Explain why counting nodes per level gives the wrong answer
  • Give O(n) time and O(w) space
Key Topics to Cover
binary trees
BFS
heap indexing
level order
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
  • Why can heap indices overflow in a language with fixed-width integers, and how does renormalizing fix it?
  • How would you return the level number along with its width?
  • How would you compute the width if null gaps should not count?
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

Input: root of a non-empty binary tree. Output: an integer, the maximum over levels of (rightmost position minus leftmost position plus one), where positions are those the nodes would have in a comple...

High-Level Design

Run a level-order BFS carrying each node's position. Within a level, the first and last queue entries give the span. To keep numbers small, subtract the level's first position from every child positio...


Submit Your Answer
Markdown supported