LearnFrenzy
Sign in Take a free test

Python Coding Interview Questions · Exercise 2 of 5

Python Coding Interview Questions Predict the Output

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Ten output questions built on the tools used in coding rounds: heapq, deque, bisect, Counter, sorting keys and matrix tricks. Every program was run on Python 3, so the answers are the real output.

Question 1 Medium

What is the output of the following Python program?

main.py
import heapq
h = []
for x in [5, 1, 8, 3]:
    heapq.heappush(h, x)
print(h[0], [heapq.heappop(h) for _ in range(len(h))], heapq.nlargest(2, [5, 1, 8, 3]))
A 5 [5, 1, 8, 3] [8, 5]
B 1 [1, 3, 5, 8] [8, 5]
C 1 [8, 5, 3, 1] [8, 5]
D 8 [1, 3, 5, 8] [5, 8]
Question page & discussion
Question 2 Medium

What is the output of the following Python program?

main.py
from collections import deque
d = deque([1, 2, 3, 4])
d.rotate(1)
d.appendleft(0)
d.pop()
print(list(d), d.popleft(), d[1])
A [0, 1, 2, 3] 0 2
B [4, 1, 2, 3] 4 2
C [0, 4, 1, 2, 3] 0 1
D [0, 4, 1, 2] 0 1
Question page & discussion
Question 3 Medium

What is the output of the following Python program?

main.py
import bisect
a = [10, 20, 20, 30]
print(bisect.bisect_left(a, 20), bisect.bisect_right(a, 20), bisect.bisect_left(a, 25), bisect.bisect_left(a, 5))
A 1 2 3 0
B 2 3 3 0
C 1 3 3 0
D 1 3 2 -1
Question page & discussion
Question 4 Medium

What is the output of the following Python program?

main.py
from collections import Counter
c = Counter("abracadabra")
print(c.most_common(3), c["z"], sum(c.values()))
A [('a', 5), ('r', 2), ('b', 2)] 0 11
B [('a', 5), ('b', 2), ('r', 2)] KeyError
C [('a', 5), ('b', 2), ('c', 1)] 0 11
D [('a', 5), ('b', 2), ('r', 2)] 0 11
Question page & discussion
Question 5 Medium

What is the output of the following Python program?

main.py
nums = [4, 1, 2, 2, 4, 4, 3]
freq = {n: nums.count(n) for n in nums}
print(sorted(freq, key=lambda n: (-freq[n], n)))
A [1, 2, 3, 4]
B [4, 2, 3, 1]
C [2, 4, 1, 3]
D [4, 2, 1, 3]
Question page & discussion
Question 6 Hard

What is the output of the following Python program?

main.py
m = [[1, 2, 3],
     [4, 5, 6]]
print([list(r) for r in zip(*m)])
print([list(r) for r in zip(*m[::-1])])
A [[1, 4], [2, 5], [3, 6]] [[1, 4], [2, 5], [3, 6]]
B [[4, 1], [5, 2], [6, 3]] [[1, 4], [2, 5], [3, 6]]
C [[1, 2, 3], [4, 5, 6]] [[4, 5, 6], [1, 2, 3]]
D [[1, 4], [2, 5], [3, 6]] [[4, 1], [5, 2], [6, 3]]
Question page & discussion
Question 7 Medium

What is the output of the following Python program?

main.py
from itertools import accumulate
nums = [3, 1, 4, 1, 5, 9]
pre = [0] + list(accumulate(nums))
print(pre, pre[4] - pre[1], pre[6] - pre[3])
A [3, 4, 8, 9, 14, 23] 6 15
B [0, 3, 4, 8, 9, 14, 23] 8 14
C [0, 3, 4, 8, 9, 14, 23] 5 15
D [0, 3, 4, 8, 9, 14, 23] 6 15
Question page & discussion
Question 8 Easy

What is the output of the following Python program?

main.py
s = "  the sky  is blue "
print(repr(" ".join(s.split()[::-1])), repr(s.strip()[::-1][:4]))
A 'blue is sky the' 'eulb'
B 'eulb si yks eht' 'eulb'
C 'blue is sky the' 'eulb'
D 'blue is sky the' ' eul'
Question page & discussion
Question 9 Hard

What is the output of the following Python program?

main.py
def check(n):
    print("checking", n)
    return n > 2

print(any(check(n) for n in [1, 3, 5]))
A checking 1 checking 3 checking 5 True
B checking 1 checking 3 True
C True
D checking 1 True
Question page & discussion
Question 10 Medium

What is the output of the following Python program?

main.py
words = ["eat", "tea", "tan", "ate", "nat", "bat"]
groups = {}
for w in words:
    groups.setdefault("".join(sorted(w)), []).append(w)
print(list(groups.values()), len(groups))
A [['eat', 'tea', 'tan', 'ate', 'nat', 'bat']] 1
B [['ate', 'eat', 'tea'], ['nat', 'tan'], ['bat']] 3
C [['eat'], ['tea'], ['tan'], ['ate'], ['nat'], ['bat']] 6
D [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']] 3
Question page & discussion
Up next · Exercise 3Find the Bug10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links