Data Structures & AlgorithmsModerateCAT · TCS NQT
Problem: How many integers from 1 to 1000 are perfect squares or perfect cubes (counted once even if both)?
- A. 41
- B. 31
- C. 35
- D. 38
Correct answer: D. 38
31 squares + 10 cubes − 3 sixth powers (1, 64, 729) = 38. Not removing the overlap gives 41, squares alone give 31, and subtracting the overlap twice gives 35.
More Coding Interview Problems questions
- Problem: An undirected graph has edges 0-1, 1-2, 2-3, 3-4, 4-0, 1-5, 5-6, 6-2. What is the length of the shortest cycle that passes…
- Problem: In a directed graph with arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, the strongly connected components are formed. What is the size of the…
- Problem: The tree with edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7, 6-8 is rooted at node 1. How many nodes have exactly two neighbours (degree 2)…
- Problem: How many occurrences of the substring "ana" are in "bananarama" (overlapping occurrences count)?
- Problem: How many distinct strings formed by permuting the letters of "aab" are palindromes?
- Problem: Find the length of the shortest substring of "bbaac" that contains both letters 'a' and 'b' at least once.