Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: Count the strings of length 4 over the alphabet {a, b, c} that contain no two adjacent equal characters and do not contain the substring "ab".
- A. 13
- B. 24
- C. 55
- D. 8
Correct answer: A. 13
Brute force over the 81 strings (or a DP on the last letter) finds 13. Ignoring the 'ab' ban gives 3 · 2³ = 24, ignoring the adjacency rule gives 55, and also banning 'ba' gives 8.
More Coding Interview Problems questions
- Problem: The number of inversions in a permutation of 1..8 given as [4, 7, 1, 8, 2, 6, 3, 5] is what value?
- Problem: On a tree with 9 nodes labelled 1–9, edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7, 6-8, 8-9, count the unordered pairs of nodes whose path…
- Problem: A string is a palindrome-partition target: what is the minimum number of cuts needed to split "abbacxcaxyyx" into palindromic…
- Problem: A word-ladder graph: vertices are words of length 3 from {cat, cot, cog, dot, dog, hat, hot} and two words are adjacent when they…
- Problem: Count the subarrays of [2, 1, 3, 2, 1, 4, 2] that have exactly 2 distinct values.
- Problem: Start at 1 and apply the rules: multiply by 3 if the number is even, add 5 if it is odd. Apply 9 steps. What is the final value?