Data Structures & AlgorithmsModerateTCS NQT · number puzzle
Problem: How many 4-digit numbers (from 1000 to 9999) are palindromes? Run the code.
Problem: How many 4-digit numbers (from 1000 to 9999) are palindromes? Run the code.
print(sum(1 for x in range(1000, 10000) if str(x) == str(x)[::-1]))What is printed?
- A. 90
- B. 100
- C. 99
- D. 80
Correct answer: A. 90
The first digit matches the last (9 choices for the first digit) and the middle two digits must mirror each other (10 choices), giving 90.
More Coding Interview Problems questions
- Problem: What is the time complexity of building a suffix tree of a string of length n over a fixed alphabet with Ukkonen's online…
- Problem: A 3×4 grid has walls '#' at (0,2), (1,1), (2,3). Moves go up, down, left and right through open cells. Starting from (0,0), how…
- Problem: A directed graph has arcs 0→1 (0), 1→2 (3), 0→2 (5), 2→3 (0), 1→3 (7), 3→4 (2), 2→4 (4), 4→5 (1), 0→5 (9). Arc weights may be…
- Problem: A tree on 6 nodes has edges 1-2, 1-3, 1-4, 4-5, 4-6. How many pairs of edges share a common endpoint (that is, unordered pairs of…
- Problem: The string "cbaebabacd" is scanned with windows of length 3. How many windows are anagrams of "abc" (same letters, same counts…
- Problem: The string "aaaaa" is split into non-overlapping occurrences of "aa" from left to right (greedily taking each match). How many…