hirecept

Python: Interview questions with runnable solutions

Python interviews for freshers and early-career roles check the language basics first, then data structures and object-oriented ideas, then a few short coding problems. This kit works for any employer, whether the job is backend, data or automation.

20 questions · 3 rounds · updated Nov 2026

How the interview works

  1. Language basics: Types, mutability, functions, decorators, generators, exceptions.
  2. Data structures and OOP: Dicts, sets, comprehensions, classes, inheritance.
  3. Coding: Short string and list problems, sometimes a small API or file task.

Sample questions

  • 01

    What is the difference between a shallow copy and a deep copy?

    MediumLanguage

    Model answer

    What the interviewer checks

    A concrete nested example showing the bug.

    How to answer

    Assignment (`b = a`) copies the reference, so both names point to one object. • Shallow copy (`a.copy()`, `list(a)`, `copy.copy(a)`, `a[:]`): a new outer container whose elements are the same objects. • Deep copy (`copy.deepcopy(a)`): recursively copies the nested objects too. ```python a = [[1, 2], [3]] b = a.copy() b[0].append(99) print(a) # [[1, 2, 99], [3]] the inner list is shared c = copy.deepcopy(a) c[0].append(7) print(a) # unchanged ``` The same trap: `grid = [[0] * 3] * 3` creates three references to one row. Use `[[0] * 3 for _ in range(3)]`.

  • 02

    def add(x, items=[]): items.append(x) return items print(add(1)); print(add(2)) What is printed?

    MediumLanguage

  • 03

    Write a decorator that retries a function up to 3 times when it raises an exception, and logs how long each call took.

    MediumFunctions

    Approach and solution

    Approach

    A decorator is a function that takes a function and returns a wrapper. Use functools.wraps to keep the original name and docstring. A decorator factory (a function returning the decorator) makes the retry count configurable.

    import functools, time
    
    def retry(times=3):
        def deco(fn):
            @functools.wraps(fn)
            def wrapper(*args, **kwargs):
                for attempt in range(1, times + 1):
                    start = time.perf_counter()
                    try:
                        return fn(*args, **kwargs)
                    except Exception as e:
                        if attempt == times:
                            raise
                        print(f"{fn.__name__} attempt {attempt} failed: {e}")
                    finally:
                        print(f"{fn.__name__} took {time.perf_counter() - start:.3f}s")
            return wrapper
        return deco
    
    calls = {"n": 0}
    @retry(times=3)
    def flaky():
        calls["n"] += 1
        if calls["n"] < 3:
            raise ValueError("not yet")
        return "ok"
    
    assert flaky() == "ok" and calls["n"] == 3
    assert flaky.__name__ == "flaky"

    Complexity: Adds a constant overhead per call.

17 more questions, members only

  • Language fundamentals8
  • Data structures and OOP4
  • Coding and practical8

Members also get how to answer each question and a preparation plan for this company.

See plans from ₹399

Every question and answer is written by Hirecept and the code is run before it is published. How a company runs its process comes from candidate reports and public pages, and companies change it, so treat that part as a guide, not a promise.