20 questions · untimed

Python interview: Coding round (Python)

Pick an option and check it. The explanation shows after each answer. Take as long as you like; nothing is timed and nothing is sent.

20 questions

Question 1 · easy

What does this Python 3.12 program print?

def reverse_words(s):
    return " ".join(reversed(s.split()))

print(reverse_words("keep calm and code"))

Question 2 · medium

What does this Python 3.12 program print?

def move_zeros(nums):
    w = 0
    for i in range(len(nums)):
        if nums[i] != 0:
            nums[w] = nums[i]
            w += 1
    for i in range(w, len(nums)):
        nums[i] = 0
    return w

nums = [0, 1, 0, 3, 12]
w = move_zeros(nums)
print(w, nums)

Question 3 · easy

What does this Python 3.12 program print?

def is_pal(s):
    t = [c.lower() for c in s if c.isalnum()]
    return t == t[::-1]

print(is_pal("Never odd or even"), is_pal("Python"))

Question 4 · medium

This function should reverse a list in place, but it prints ['a', 'b', 'c', 'd'] unchanged. Which fix is right?

def reverse_in_place(chars):
    n = len(chars)
    for i in range(n):
        chars[i], chars[n - 1 - i] = chars[n - 1 - i], chars[i]
    return chars

print(reverse_in_place(list("abcd")))

Question 5 · hard

What does this Python 3.12 program print?

def max_sub(nums):
    best = cur = nums[0]
    for x in nums[1:]:
        cur = max(x, cur + x)
        best = max(best, cur)
    return best

print(max_sub([-2, 1, -3, 4, -1, 2, 1, -5, 4]), max_sub([-3, -1, -2]))

Question 6 · medium

What does this Python 3.12 program print?

def two_sum(nums, target):
    seen = {}
    for i, x in enumerate(nums):
        if target - x in seen:
            return [seen[target - x], i]
        seen[x] = i
    return []

print(two_sum([3, 2, 4], 6))

Question 7 · easy

What does this Python 3.12 program print?

from collections import Counter

def first_unique(s):
    counts = Counter(s)
    for i, c in enumerate(s):
        if counts[c] == 1:
            return i
    return -1

print(first_unique("swiss"), first_unique("aabb"))

Question 8 · easy

This anagram check prints True, but "aab" and "abb" are not anagrams. Which fix is right?

def is_anagram(a, b):
    return set(a) == set(b)

print(is_anagram("aab", "abb"))

Question 9 · medium

In the group-anagrams problem you write groups[key].append(word), where groups is a defaultdict(list). Why does key = tuple(sorted(word)) work while key = sorted(word) raises TypeError?

Question 10 · hard

Given an unsorted list of integers, return the length of the longest run of consecutive values (for [100, 4, 200, 1, 3, 2] the answer is 4, from 1, 2, 3, 4). The interviewer asks for O(n) average time. Which approach meets it?

Question 11 · medium

What does this Python 3.12 program print?

def longest_unique(s):
    last = {}
    start = best = 0
    for i, c in enumerate(s):
        if c in last and last[c] >= start:
            start = last[c] + 1
        last[c] = i
        best = max(best, i - start + 1)
    return best

print(longest_unique("pwwkew"), longest_unique("abba"))

Question 12 · easy

What does this Python 3.12 program print?

def count_pairs(nums, target):
    i, j, count = 0, len(nums) - 1, 0
    while i < j:
        s = nums[i] + nums[j]
        if s == target:
            count += 1
            i += 1
            j -= 1
        elif s < target:
            i += 1
        else:
            j -= 1
    return count

print(count_pairs([1, 2, 3, 4, 5, 6, 7], 8))

Question 13 · medium

This should return the largest sum of any 3 consecutive elements (16, from 1, 7, 8 or 7, 8, 1), but it prints 19. Which fix is right?

def max_sum_k(nums, k):
    window = sum(nums[:k])
    best = window
    for i in range(k, len(nums)):
        window += nums[i] - nums[i - k + 1]
        best = max(best, window)
    return best

print(max_sum_k([4, 2, 1, 7, 8, 1, 2, 8, 1, 0], 3))

Question 14 · easy

A list of integers is sorted in ascending order. You must say whether any two of them add up to target in O(n) time and O(1) extra space. Which approach fits?

Question 15 · medium

This sliding-window solution has a while loop inside a for loop. Why is it still O(n) for a string of length n?

def longest_unique(s):
    window = set()
    left = best = 0
    for right, c in enumerate(s):
        while c in window:
            window.remove(s[left])
            left += 1
        window.add(c)
        best = max(best, right - left + 1)
    return best

Question 16 · medium

list.append in CPython is described as amortised O(1). What does that mean?

Question 17 · medium

What is the time complexity of this function for a list of n numbers?

def total(nums):
    if not nums:
        return 0
    return nums[0] + total(nums[1:])

Question 18 · hard

What does this Python 3.12 program print?

n = 16
count = 0
i = 1
while i < n:
    for j in range(i):
        count += 1
    i *= 2
print(count)

Question 19 · easy

This duplicate check gives the right answer but times out on a list of 100,000 numbers. Which change makes it O(n) on average?

def has_duplicate(nums):
    seen = []
    for x in nums:
        if x in seen:
            return True
        seen.append(x)
    return False

Question 20 · easy

A breadth-first search over a graph with 100,000 nodes uses queue = [start] and takes the next node with queue.pop(0). It is slow. Which change makes both adding and removing a node O(1)?

Next: SQL round