Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: A rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 5: [], 6: [], 7: []. Children are visited in increasing order (pre-order DFS). Which node is visited 5th (the root is 1st)?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Pre-order DFS visits a node and then its children in order. Pushing children in increasing order onto a stack reverses the visiting order, which is the usual slip.
More Coding Interview Problems questions
- Problem: Insert the words "car", "cat", "care", "dog", "do", "dot" into an empty trie (one node per character, root has no letter). How…
- Problem: Count the occurrences of the pattern "aba" in "abababa", where occurrences may overlap.
- Problem: The words "listen", "silent", "enlist", "google", "gogole", "cat", "act", "tac", "dog" are grouped by anagram class (words with…
- Problem: In the binary string "1101001110100110", what is the length of the longest contiguous substring containing equal numbers of 0s…
- Problem: For the string "abcab", count the subsequences of length 3 that are palindromes (choose 3 indices i < j < k with s[i] = s[k]; the…
- Problem: What is the minimum number of characters that must be inserted anywhere in "leetcode" to make it a palindrome?