Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
What is printed? (2-D array in row-major order)
What is printed? (2-D array in row-major order)
R, C = 3, 4
base, size = 1000, 4
def addr(i, j, row_major=True):
return base + size * (i * C + j if row_major else j * R + i)
print(addr(2, 1), addr(2, 1, False))- A. 1036 1020
- B. 1036 1028
- C. 1020 1036
- D. 1024 1020
Correct answer: A. 1036 1020
Row-major: offset = i·C + j = 2·4 + 1 = 9 elements → 1000 + 36. Column-major: offset = j·R + i = 1·3 + 2 = 5 → 1000 + 20. C and Python (NumPy default) are row-major; Fortran is column-major.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is printed? (Python string immutability and methods) python s = "Hello World" t = s.replace("o", "0", 1) u = s.swapcase()[::2]…
- What is printed? (queue simulation – hot potato with step k) python from collections import deque q = deque(range(1, 8)); out = [] while…
- What is printed? (Josephus recurrence) python def josephus(n, k): r = 0 for m in range(2, n + 1): r = (r + k) % m return r + 1…
- What is printed? (hash set for pairs with a given difference) python def pairsdiff(a, k): s = set(a) return sorted((x, x + k) for x in s…
- What is printed? (Python dict ordering and update) python d = {"b": 1, "a": 2} d["c"] = 3 d["b"] = 4 del d["a"] d["a"] = 5…
- What is printed? (prefix sums for range queries) python a = [3, -2, 5, 1, -4, 6] P = [0] for x in a: P.append(P[-1] + x) q = [(0, 2), (1…