Advanced Interview Questions

Python InterviewFree Lesson

Advertisement

Advanced Interview Questions

Complex coding problems, system design, and advanced concepts.

Overview

Master advanced interview problems.

System Design Questions

# Design a URL shortener
import hashlib
import string

class URLShortener:
    def __init__(self):
        self.urls = {}
        self.counter = 0
    
    def shorten(self, url: str) -> str:
        self.counter += 1
        short_id = self.base62_encode(self.counter)
        self.urls[short_id] = url
        return f"https://short.url/{short_id}"
    
    def expand(self, short_url: str) -> str:
        short_id = short_url.split('/')[-1]
        return self.urls.get(short_id)
    
    def base62_encode(self, num: int) -> str:
        chars = string.ascii_letters + string.digits
        result = []
        while num:
            result.append(chars[num % 62])
            num //= 62
        return ''.join(reversed(result)) or '0'

# Usage
shortener = URLShortener()
short_url = shortener.shorten("https://www.example.com/very/long/url")
print(short_url)
print(shortener.expand(short_url))

Advanced Algorithms

# LRU Cache implementation
class LRUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.cache = {}
        self.order = []
    
    def get(self, key: int) -> int:
        if key in self.cache:
            self.order.remove(key)
            self.order.append(key)
            return self.cache[key]
        return -1
    
    def put(self, key: int, value: int) -> None:
        if key in self.cache:
            self.order.remove(key)
        elif len(self.cache) >= self.capacity:
            oldest = self.order.pop(0)
            del self.cache[oldest]
        
        self.cache[key] = value
        self.order.append(key)

# Usage
cache = LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
print(cache.get(1))  # 1
cache.put(3, 3)  # evicts key 2
print(cache.get(2))  # -1

Practice

Solve: Implement a thread-safe singleton pattern.

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement