Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
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 = [[50, 81, 32, 23], [30, 61, 32, 23], [60, 51, 32, 43]]
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. [60, 51, 32, 30]
- B. [32, 51, 60, 30]
- C. [32, 51, 30, 60]
- D. [32, 60, 51, 30]
Correct answer: B. [32, 51, 60, 30]
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: [32, 51, 60, 30].
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 chained hash table stores n = 2,000 keys in m = 500 buckets with simple uniform hashing. What is the expected number of key comparisons…
- Bars of width 1 have heights [5, 5, 7, 8, 6, 5, 2, 8]. What is the area of the largest rectangle that fits under the histogram (computed…
- Product of all elements except self, using prefix and suffix products (no division). What is printed? python a = [2, 3, 3, 2, -1] n =…
- What does this loop-counting code print? (Python) python n, c = 16, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- For the sorted array [1, 3, 4, 6, 6, 9, 9, 11, 13, 14, 14, 15] (0-based), what are lowerbound(14) (first index with value ≥ 14) and…
- Records are sorted with a composite key. What is printed? python recs = [('kabir', 80), ('dev', 70), ('meera', 70), ('ravi', 70), ('zoya'…