Sum Article Quality Scores Within a Range Using a BST

Last updated: November 29, 2026

Quick Overview

Toutiao stores article quality scores in a binary search tree; given the root and bounds low and high, return the sum of all scores in [low, high], visiting as few nodes as possible.

ByteDance
Coding & Algorithms
Software Engineer
ByteDance
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Toutiao stores article quality scores in a binary search tree; given the root and bounds low and high, return the sum of all scores in [low, high], visiting as few nodes as possible.

This screening question in ByteDance's style checks that you actually use the BST property to prune, not just traverse everything. Interviewers often follow up on how to answer many range queries quickly.

What the Interviewer Expects
  • Use the BST ordering to skip whole subtrees
  • Include both bounds (closed interval)
  • Write an iterative traversal
  • Explain O(h + k) behavior where k is the number of nodes in range
  • Handle an empty tree
Key Topics to Cover
binary search tree
pruning
range query
iterative 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
  • Answer a million range queries on a static tree.
  • Scores update frequently: which structure supports fast updates and range sums?
  • Return the k-th largest score in the range.
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 BST with distinct integer scores, and integers low <= high. Output: the sum of node values v with low <= v <= high. Empty tree returns 0....

High-Level Design

At each node, if its value is below low, everything in its left subtree is smaller still, so only go right. If it is above high, only go left. Otherwise count it and explore both sides. This prunes su...


Submit Your Answer
Markdown supported