L2 · Working engineerObject-oriented Python~5 min · 3 tests#78

Implement the Singleton Pattern

Implement a Singleton in Python with __new__ so every Singleton() call returns the same object. Compare __new__, a decorator and a module-level instance.

The problem

Make Singleton() always return the same object: Singleton() is Singleton() must be True.

Data set on one reference must be visible through any other.

Examples

  1. Example 1

    Input

    Singleton() is Singleton()

    Expected output

    True
  2. Example 2

    Input

    (lambda a, b: (setattr(a, 'x', 42), b.x)[1])(Singleton(), Singleton())

    Expected output

    42

+ 1 hidden test on Submit.

Edge cases to ask about

  • init runs every time
  • Thread safety

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · Singleton
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Override __new__O(1)O(1)new creates the object; return the cached one.
    bestModule-level instanceO(1)O(1)Python modules are already singletons — often the most Pythonic answer.
    Walkthrough of the optimal approach (try it yourself first)

    __new__ is what actually creates the instance. Cache the first one on the class and return it on every later call.

    Caveat: __init__ still runs on every Singleton() call, so avoid re-initialising state there. In Python, a module-level object is often the simplest singleton — mention it, and that singletons make testing harder.

    Complexity: O(1) time, O(1) space. One check and a cached reference.

    Reveal the reference solution
    class Singleton:
        _instance = None
    
        def __new__(cls):
            if cls._instance is None:
                cls._instance = super().__new__(cls)
            return cls._instance

    Follow-ups interviewers ask

    • Make it thread-safe.
    • Write a @singleton class decorator.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Implement the Singleton Pattern in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. One check and a cached reference.

    What is the brute-force approach, and how do you optimise it?

    Override __new__: O(1) time, O(1) space. __new__ creates the object; return the cached one. Module-level instance: O(1) time, O(1) space. Python modules are already singletons — often the most Pythonic answer.

    What follow-up questions do interviewers ask about Implement the Singleton Pattern?

    Make it thread-safe. Write a @singleton class decorator.