Data Structures & AlgorithmsModerateFlipkart/Uber OA · placement
Rotate a square matrix 90° clockwise in place by transposing and then reversing each row. What is printed?
Rotate a square matrix 90° clockwise in place by transposing and then reversing each row. What is printed?
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
n = len(m)
for i in range(n):
for j in range(i + 1, n):
m[i][j], m[j][i] = m[j][i], m[i][j]
for row in m: row.reverse()
print(m[0], m[2])- A. [3, 6, 9] [1, 4, 7]
- B. [7, 1, 4] [9, 6, 3]
- C. [7, 4, 1] [9, 3, 6]
- D. [7, 4, 1] [9, 6, 3]
Correct answer: D. [7, 4, 1] [9, 6, 3]
Transpose then reverse each ROW gives a clockwise rotation; transpose then reverse the row ORDER gives anticlockwise. Output: [7, 4, 1] [9, 6, 3].
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
- For the binary array [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1], replace 0 by −1 and use prefix sums with a first-occurrence map. What is the…
- Which application does NOT naturally use a stack?
- Two numbers are stored as linked lists with digits in REVERSE order (units first). What does this addition print? python def add(a, b): d…
- How many contiguous subarrays of [5, 4, 4, 3, 2, 5, 2, 2] have a sum between 7 and 11 inclusive? (All values positive: count atMost(11) −…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- Two numbers are stored as linked lists with digits in REVERSE order (units first). What does this addition print? python def add(a, b): d…