PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Applovin

Count invalid nodes in a BST

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of binary search tree invariants and the ability to identify nodes that violate ancestor-imposed value ranges, testing competency with tree data structures and constraint propagation in the coding & algorithms domain.

  • hard
  • Applovin
  • Coding & Algorithms
  • Software Engineer

Count invalid nodes in a BST

Company: Applovin

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given the root of a binary tree. A valid Binary Search Tree (BST) must satisfy: for every node with value `v`, **all** values in its left subtree are strictly `< v`, and **all** values in its right subtree are strictly `> v` (i.e., the constraint is with respect to all ancestors, not just the parent). Instead of returning a boolean, return the **number of nodes that violate the BST rule**. ### Input - The root of a binary tree. - Node values are integers. ### Output - An integer: the count of nodes that are part of at least one BST-order violation (i.e., nodes whose value is not within the valid `(low, high)` range implied by ancestors). ### Notes / Clarifications - Use strict inequality (no duplicates allowed in a valid BST). - Counting is per node: if a node violates constraints from multiple ancestors, it is still counted **once**. ### Constraints (typical interview assumptions) - Number of nodes `n` up to ~`10^5`. - Aim for `O(n)` time.

Quick Answer: This question evaluates understanding of binary search tree invariants and the ability to identify nodes that violate ancestor-imposed value ranges, testing competency with tree data structures and constraint propagation in the coding & algorithms domain.

Applovin logo
Applovin
Nov 3, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0

You are given the root of a binary tree. A valid Binary Search Tree (BST) must satisfy: for every node with value v, all values in its left subtree are strictly < v, and all values in its right subtree are strictly > v (i.e., the constraint is with respect to all ancestors, not just the parent).

Instead of returning a boolean, return the number of nodes that violate the BST rule.

Input

  • The root of a binary tree.
  • Node values are integers.

Output

  • An integer: the count of nodes that are part of at least one BST-order violation (i.e., nodes whose value is not within the valid (low, high) range implied by ancestors).

Notes / Clarifications

  • Use strict inequality (no duplicates allowed in a valid BST).
  • Counting is per node: if a node violates constraints from multiple ancestors, it is still counted once .

Constraints (typical interview assumptions)

  • Number of nodes n up to ~ 10^5 .
  • Aim for O(n) time.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Applovin•More Software Engineer•Applovin Software Engineer•Applovin Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.