Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: In the grid of 0s and 1s below, count the number of 2×2 all-ones squares. 1110 1111 0111 1111
- A. 7
- B. 21
- C. 6
- D. 9
Correct answer: C. 6
Check each of the 9 top-left positions: 6 of the 2 × 2 blocks are all ones. Also counting the single 3 × 3 all-ones block gives 7, counting all-ones squares of every size gives 21, and 9 counts every 2 × 2 position regardless of content.
More Coding Interview Problems questions
- Problem: A string s = "abacaba" is given. How many of its substrings are palindromes of odd length (counting index ranges, so repeated…
- Problem: A directed graph has 4 vertices (0–3) and arcs with weights: 0→1 (4), 1→2 (−3), 0→2 (2), 2→3 (1), 1→3 (5), 3→1 (−1). Start with…
- Array a = [4, 2, 6, 1, 5, 3, 3, 9, 2]. How many contiguous subarrays of length at least 3 have a sum divisible by 6?
- Array a = [3, 5, 8, 2, 7, 10, 4]. How many pairs of positions i < j have a[i] + a[j] equal to some value located strictly after position j?
- Sorted array b = [1, 2, 3, 4, 5, 7, 8, 9]. What is the largest product b[i] b[j] over index pairs with j - i >= 3?
- Sorted array [1, 3, 4, 6, 8, 9, 11]. How many index triples i < j < k satisfy c[k] - c[i] <= 4?