Sum Order Values Inside a Price Band Using a Search Tree

Last updated: November 29, 2026

Quick Overview

Order values are stored in a binary search tree. Given low and high, return the sum of all values in the inclusive range [low, high], skipping subtrees that cannot contribute.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Order values are stored in a binary search tree. Given low and high, return the sum of all values in the inclusive range [low, high], skipping subtrees that cannot contribute.

Amazon online assessments include short BST items like this to check you prune instead of visiting every node. Think of a seller dashboard summing revenue for orders within a price band.

What the Interviewer Expects
  • Use the BST property to skip whole subtrees
  • Write an iterative stack-based traversal
  • Use inclusive bounds correctly
  • Explain best and worst case complexity
  • Mention how augmenting nodes with subtree sums enables O(h) queries
Key Topics to Cover
BST
range query
pruning
DFS
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 support many range-sum queries with updates efficiently?
  • What if the tree held duplicate values?
  • How would you return the count instead of the sum?
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: BST root (possibly empty) with unique integers, and integers low <= high. Output: sum of node values v with low <= v <= high....

High-Level Design

DFS with pruning. If a node's value is below low, its left subtree is entirely below low, so only the right child is explored. If the value is above high, only the left child matters. Otherwise add it...


Submit Your Answer
Markdown supported