Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Insert the keys 50, 20, 70, 10, 30, 60, 90, 25, 35, 65 into a binary search tree in this order. How many keys in the tree lie in the closed range [25, 65]?
- A. 4
- B. 6
- C. 10
- D. 5
Correct answer: B. 6
A range search visits only subtrees that can hold keys in [25, 65]: 25, 30, 35, 50, 60, 65 → 6. Excluding both endpoints gives 4, using a half-open range gives 5, and 10 counts every key.
More Coding Interview Problems questions
- Problem: Count the ways to segment "catsanddog" into a sequence of words, each taken from the dictionary {cat, cats, and, sand, dog}…
- Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?
- Problem: A binary string "1100101011" is to be changed so that it alternates (no two adjacent characters are equal). Each operation flips…
- Problem: Count the ordered pairs of words (i, j) with i ≠ j in ["abc", "cba", "bca", "abc", "cab", "xyz"] such that the second word is the…
- Problem: For the string "AABABBA" and k = 1, you may replace at most one character with any other letter. What is the length of the…
- Problem: Count the substrings of length 3 in "xyzzaz" that contain three distinct characters.