Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: Count the sequences of length 5 over {0, 1, 2} with no two adjacent equal digits and whose first digit is 0.
- A. 48
- B. 32
- C. 16
- D. 8
Correct answer: C. 16
The first digit is fixed as 0 and each later digit has 2 choices (anything but its predecessor), so 2⁴ = 16. Not fixing the first digit gives 3 · 2⁴ = 48, giving all five positions 2 choices gives 32, and 2³ = 8 is length 4.
More Coding Interview Problems questions
- Problem: The sorted array [1, 4, 4, 9, 12, 12, 15, 20] (0-indexed) may contain repeated values. Using a lower-bound search, which returns…
- Problem: How many subarrays of [1, 2, 2, 3] have max minus min at most 1? Run the code. python a = [1, 2, 2, 3] print(sum(1 for i in…
- Problem: How many peaks (elements strictly greater than both neighbours, excluding the two ends) does [1, 3, 2, 4, 1, 5] have? Run the…
- Problem: What does this run-length encoder print for 'aaabccdd'? python s = 'aaabccdd' out = [] i = 0 while i < len(s): j = i while j <…
- Problem: Check bracket balance for '([]{})' and '([)]' with a stack. What is printed? python def ok(s): pairs = {')': '(', ']': '[', '}'…
- Problem: Sum the values of the next greater element of each position in [2, 1, 4, 3], where a position with no greater element contributes…