BackhardGreedyNetflixRazorpay

Subtree Height Evaluator Optimizer 2 Solution

Problem Statement

The solution should correctly implement the Gas Station Circuit methodology to calculate the maximum height of the binary tree that can be formed by the given array.

Example 1
Input
[3, 2, 1, 4, 5, 6, 7, 8, 9]
Output
3

Explanation: Step-by-step: with input [3, 2, 1, 4, 5, 6, 7, 8, 9], we first find the middle element 5. The left subtree height is 2 and the right subtree height is 2. The minimum of the two is 2, so the maximum height of the binary tree is 2 + 1 = 3.

Example 2
Input
[10, 20, 30, 40, 50]
Output
2

Explanation: Step-by-step: with input [10, 20, 30, 40, 50], we first find the middle element 30. The left subtree height is 1 and the right subtree height is 1. The minimum of the two is 1, so the maximum height of the binary tree is 1 + 1 = 2.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N) or O(N log N)
  • Space Complexity: O(N) or O(1)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Subtree Height Evaluator Optimizer 2 — Problem Statement & Solution Guide

GreedyHardGas Station Circuit
TimeO(n)
|
SpaceO(1)

Problem Description

The solution should correctly implement the Gas Station Circuit methodology to calculate the maximum height of the binary tree that can be formed by the given array.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Subtree Height Evaluator Optimizer 2"

hard

WHY DOES IT MATTER?

The greedy gas‑station pattern transforms a seemingly combinatorial tree‑building problem into a feasibility check on a circular sequence, enabling interviewers to assess a candidate's ability to abstract problem constraints and apply classic linear‑time paradigms.

OPTIMIZATION CHALLENGE

The key insight is that a negative running surplus invalidates all indices before the current position as potential starts, allowing us to discard large swaths of candidates in O(1) time per element, reducing the naive O(n^2) or exponential search to O(n).

REAL-WORLD CONNECTION

Think of a distributed log replication system where each node contributes bandwidth (fuel) and must forward logs (cost). Determining a starting node that can propagate the entire log without stalling mirrors the subtree height evaluator, highlighting load‑balancing and fault‑tolerance concepts.

When coding, first compute totalContribution and totalCost; if the former < the latter, return -1 immediately. Then use two variables, curSurplus and startIdx, resetting startIdx to i+1 whenever curSurplus < 0. This pattern is bullet‑proof and avoids off‑by‑one errors.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem can be reframed as a circular‑array feasibility test that is famously solved by the Gas Station Circuit algorithm. Each array element represents the "height contribution" a node can provide when placed as a parent, while the cost to attach the next node is the minimum height needed to keep the binary tree balanced. A naive scan that tries every possible root and builds the tree recursively explodes to O(n·2^n) because each choice spawns two sub‑trees, making it infeasible for n > 10^5. The greedy insight is that if the total sum of contributions (fuel) is at least the total sum of required heights (cost), a valid construction exists, and the unique starting index where the running deficit never goes negative yields the maximal achievable height. By traversing the array once, maintaining a cumulative surplus, and resetting the start when the surplus dips below zero, we obtain the optimal height in linear time, mirroring the classic gas‑station proof that a single pass suffices to locate the feasible start point.

Interview Questions on This Problem

Q1How does the Gas Station Circuit algorithm guarantee a linear‑time solution for determining if a binary tree of maximum height can be built from a circular array of height contributions?

The algorithm relies on two facts: (1) if the total contribution >= total required height, a solution exists, and (2) any prefix where the cumulative surplus becomes negative cannot be part of the optimal start, so we shift the start to the next index. By scanning once and resetting the start whenever the running sum is negative, we ensure O(n) time and O(1) extra space.

Q2Why does a brute‑force recursive construction of the tree fail on large inputs, and how does the greedy approach avoid the combinatorial explosion?

A recursive construction explores every possible parent‑child assignment, leading to a Catalan‑like number of binary‑tree shapes (≈2^n). The greedy method abstracts away the exact shape and only tracks the net height surplus, collapsing the exponential state space into a single scalar, thus eliminating the need for exponential recursion.

Q3In a fintech platform that processes streaming transaction batches, how would you adapt the subtree‑height evaluator to ensure real‑time latency guarantees?

Treat each batch as a sliding window over the transaction array. Maintain a rolling total contribution and cost, and update the start index only when the rolling surplus becomes negative. This incremental update mirrors the gas‑station logic and guarantees O(1) amortized per new transaction, meeting low‑latency SLAs.

Examples

Example 1

Input

[3, 2, 1, 4, 5, 6, 7, 8, 9]

Output

3

Explanation: Step-by-step: with input [3, 2, 1, 4, 5, 6, 7, 8, 9], we first find the middle element 5. The left subtree height is 2 and the right subtree height is 2. The minimum of the two is 2, so the maximum height of the binary tree is 2 + 1 = 3.

Example 2

Input

[10, 20, 30, 40, 50]

Output

2

Explanation: Step-by-step: with input [10, 20, 30, 40, 50], we first find the middle element 30. The left subtree height is 1 and the right subtree height is 1. The minimum of the two is 1, so the maximum height of the binary tree is 1 + 1 = 2.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N) or O(N log N)
  • Space Complexity: O(N) or O(1)

Optimal Approach & Strategy

Apply the gas‑station greedy scan: compute total surplus, then walk once resetting the start when the running surplus is negative, yielding the maximal height in linear time with constant extra space.

Brute Force Approach

Try every index as the root, recursively build left and right subtrees, and compute the height; this leads to exponential time. It also requires storing many intermediate trees, blowing up space usage.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
   if (nums.length === 0) return 0;
   let max = 0;
   for (let i = 0; i < nums.length; i++) {
       if (nums[i] < 0) return 0;
       let leftHeight = getLeftHeight(nums, i);
       let rightHeight = getRightHeight(nums, i);
       max = Math.max(max, Math.min(leftHeight, rightHeight) + 1);
   }
   return max;
}

function getLeftHeight(nums, i) {
   let left = 0;
   for (let j = i - 1; j >= 0; j--) {
       if (nums[j] < nums[i]) break;
       left++;
   }
   return left;
}

function getRightHeight(nums, i) {
   let right = 0;
   for (let j = i + 1; j < nums.length; j++) {
       if (nums[j] < nums[i]) break;
       right++;
   }
   return right;
}

Asked in Top Tech Interviews

NetflixRazorpay

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.