Data Structures & AlgorithmsHardGATE CS · DSA
Print a matrix in spiral order (right, down, left, up, repeat). What is printed?
Print a matrix in spiral order (right, down, left, up, repeat). What is printed?
m = [[90, 11, 62, 33], [40, 71, 82, 13], [10, 81, 22, 63]]
top, bot, l, r, out = 0, len(m) - 1, 0, len(m[0]) - 1, []
while top <= bot and l <= r:
out += m[top][l:r + 1]; top += 1
out += [m[i][r] for i in range(top, bot + 1)]; r -= 1
if top <= bot: out += m[bot][l:r + 1][::-1]; bot -= 1
if l <= r: out += [m[i][l] for i in range(bot, top - 1, -1)]; l += 1
print(out[6:6 + 4])- A. [81, 22, 10, 40]
- B. [22, 10, 81, 40]
- C. [10, 81, 22, 40]
- D. [22, 81, 10, 40]
Correct answer: D. [22, 81, 10, 40]
The bottom row is read right-to-left and the left column bottom-to-top; the guards stop a single remaining row or column from being read twice. Output: [22, 81, 10, 40].
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
- A 2-D array A[0..11][0..29] of 4-byte elements is stored in row-major order starting at address 1000. What is the address of A[5][11]?
- For the sorted array [1, 5, 6, 7, 7, 7, 8, 9, 9, 10, 10, 11] (0-based), what are lowerbound(9) (first index with value ≥ 9) and…
- Keys 13, 14, 24, 27, 70, 76, 91, 15 are inserted in this order into an empty hash table of size 13 with h(k) = k mod 13 and linear…
- Keys 55, 63, 47, 69, 16, 97, 39, 99, 77, 43 are inserted in this order into a chained hash table with 7 buckets, h(k) = k mod 7, and each…
- Floyd's cycle detection on a list whose last node links back into the list. What does this print (meeting value, cycle-start value, cycle…
- What does this loop-counting code print? (Python) python n, c = 70000, 0 i = 2 while i <= n: c += 1 i = i i print(c)