Interview Questions Advanced

Python InterviewFree Lesson

Advertisement

Interview Questions Advanced

System design, algorithm, and Python-specific questions.

Overview

Prepare for technical interviews.

System Design

# Design a URL shortener
import hashlib
import string

class URLShortener:
    def __init__(self):
        self.urls = {}
        self.counter = 0
    
    def shorten(self, url):
        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):
        short_id = short_url.split('/')[-1]
        return self.urls.get(short_id)
    
    def base62_encode(self, num):
        chars = string.ascii_letters + string.digits
        result = []
        while num:
            result.append(chars[num % 62])
            num //= 62
        return ''.join(reversed(result)) or '0'

Algorithm Questions

# Two Sum
def two_sum(nums, target):
    seen = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            return [seen[complement], i]
        seen[num] = i
    return []

# Valid Parentheses
def is_valid(s):
    stack = []
    mapping = {')': '(', '}': '{', ']': '['}
    
    for char in s:
        if char in mapping:
            if not stack or stack[-1] != mapping[char]:
                return False
            stack.pop()
        else:
            stack.append(char)
    
    return not stack

Practice

Solve: Find the longest substring without repeating characters.

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement