No worries… I had also shared the solutions…

Non-member link.

After giving multiple Python interviews and failing some of them too, I realized something important. Most of the challenges aren't just about syntax or basic logic.

They test how well you understand Python internally. That means you should know how things work internally…not just how to write a for loop.

I am going to share with you a few real Python interview questions that I found tricky. I'll also share how I solved them and what I learned in the process.

1. LRU Cache (Least Recently Used)

In one interview, I was asked to implement an LRU Cache from scratch. I had never written one before without using a library.

So… here's the problem: You're given a cache with a fixed size. You need to store key-value pairs. When the cache is full and a new item is added, you have to remove the least recently used item first.

At first, I tried using just a dictionary. But I quickly realized that dictionaries don't store order in a reliable way before Python 3.7. So I switched to using OrderedDict.

This is the final solution I gave in the interview:

defaultdict, OrderedDict
from collections import OrderedDict
class LRUCache:
    def __init__(self, capacity):
        self.cache = OrderedDict()
        self.capacity = capacity
    def get(self, key):
        if key not in self.cache:
            return -1
        self.cache.move_to_end(key)
        return self.cache[key]
    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)
        self.cache[key] = value
        if len(self.cache) > self.capacity:
            self.cache.popitem(last=False)

This worked. The interviewer asked some follow-up questions about time complexity.

The get and put functions both run in O(1) time because OrderedDict keeps the insertion order and supports fast deletion and reordering.

2. Detect a Cycle in a Linked List

Another common one. They gave me a singly linked list and asked if I could check whether the list had a cycle.

At first, I thought I'd just keep storing the visited nodes in a set.

seen = set()
while node:
    if node in seen:
        return True
    seen.add(node)
    node = node.next

This works, but it uses extra space. Then the interviewer said, "Can you do it without extra space?"

That's when I remembered Floyd's Cycle Detection algorithm.

def has_cycle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

This uses two pointers. If there's a loop, they'll eventually meet. Otherwise, fast will reach the end.

3. Decode Ways

This question messed me up the first time I saw it.

You're given a string containing only digits. Each digit maps to a letter: Ƈ' → 'A', ƈ' → 'B', …, བ' → 'Z'.

You have to count how many ways you can decode the string.

So ཈' can be decoded as 'AB' or 'L'. That's 2 ways.

I tried recursion first but it was too slow. So I switched to dynamic programming.

def num_decodings(s):
    if not s or s[0] == '0':
        return 0
    n = len(s)
    dp = [0] * (n + 1)
    dp[0] = 1
    dp[1] = 1

for i in range(2, n + 1):
        one_digit = int(s[i-1])
        two_digits = int(s[i-2:i])
        if 1 <= one_digit <= 9:
            dp[i] += dp[i-1]
        if 10 <= two_digits <= 26:
            dp[i] += dp[i-2]
    return dp[n]

It's not an easy problem, especially when you're under pressure.

But once I broke it down into smaller subproblems, it became manageable.

4. Custom Sorting with Comparator

This one surprised me. I was given a list of numbers and asked to sort them so that they form the largest number when concatenated.

For example: [3, 30, 34, 5, 9] should become '9534330'.

I thought I could just use the normal sort() method. But that doesn't work. You need a custom comparator.

In Python, you can use functools.cmp_to_key.

from functools import cmp_to_key
def compare(a, b):
    if a + b > b + a:
        return -1
    else:
        return 1
def largest_number(nums):
    nums = list(map(str, nums))
    nums.sort(key=cmp_to_key(compare))
    return ''.join(nums).lstrip('0') or '0'

This was one of those "think different" problems. It's not about deep algorithms, but about creative sorting logic.

Interviewers won't always ask basic Python questions. Sometimes they want to see how you think when you're given a hard, unfamiliar problem.

That's what happened with me in most of these interviews. I didn't always get them right the first time. But every time I failed, I sat down afterward and resolved them.

I believe that's the only real way to get better. You can't memorize solutions. You have to understand the patterns.

If you've struggled with interviews before, just know — I did too. A lot.

But slowly, the patterns started making sense. And now I can say that most of these problems don't scare me anymore.

Hope this helped. If you have a weird Python interview question you got stuck on… tell me. Maybe we can solve it together.

You may also like —