Question 31
MediumWhat is the output of this code?
a = [1, 2, 3, 4, ]
k =
(a[-k:] + a[:-k])
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
a = [1, 2, 3, 4, ]
k =
(a[-k:] + a[:-k])
[3, 4, 5, 1, 2]
[4, 5, 1, 2, 3]
[2, 3, 4, 5, 1]
[5, 4, 3, 2, 1]
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of finding the missing number in an array containing n − 1 distinct numbers from 1 to n, using the sum formula?
O(n²)
O(1)
O(n)
O(n log n)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [5, 3, 5, 2, 5, 5, 6]
cand, cnt = None, 0
for x in a:
if cnt == 0:
cand = x
cnt += 1 if x == cand else -1
print(cand)
2
5
Sign up free to see the answer and a step-by-step explanation.
In row-major order, at what offset is element a[i][j] of an array with C columns stored?
j × C + i
i + j
i × j
i × C + j
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [3, 1, 4, 1, 5, 9, 2, 6]
p = [0]
for x in a:
p.append(p[-1] + x)
print(p[6] - p[2])
14
19
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 2, 3, 4, 5, 6, 7]
k = 3
a = a[-k:] + a[:-k]
print(a)
[1, 2, 3, 4, 5, 6, 7]
[4, 5, 6, 7, 1, 2, 3]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [2, -3, 4, -1, -2, 1, 5, -3]
best = cur = a[0]
for x in a[1:]:
cur = max(x, cur + x)
best = max(best, cur)
print(best)
5
8
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [16, 17, 4, 3, 5, 2]
res, mx = [], float("-inf")
for x in reversed(a):
if x > mx:
res.append(x)
mx = x
print(res[::-1])
[2, 5, 17]
[16, 17, 5, 2]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [0, 1, 0, 3, 12]
j = 0
for i in range(len(a)):
if a[i] != 0:
a[i], a[j] = a[j], a[i]
j += 1
print(a)
[1, 0, 3, 12, 0]
[0, 0, 1, 3, 12]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 2, 3, 4]
n = len(a)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= a[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= a[i]
print(out)
[1, 2, 6, 24]
[24, 6, 2, 1]
Sign up free to see the answer and a step-by-step explanation.
6
3
20
25
[5, 6, 7, 1, 2, 3, 4]
[3, 2, 1, 7, 6, 5, 4]
4
7
[17, 4, 3, 5, 2]
[17, 5, 2]
[1, 3, 12, 0, 0]
[12, 3, 1, 0, 0]
[10, 10, 10, 10]
[24, 12, 8, 6]