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

Class Variables vs Instance Variables

See how a Python class variable is shared by every instance while instance variables are per-object, and how assignment can shadow it. Tested live.

The problem

  • Employee has a class variable company = "ABC", shared by all employees.
  • Each employee has an instance variable salary set in __init__.
  • Employee.rename_company(new) (a @classmethod) changes the company for everyone.

Examples

  1. Example 1

    Input

    scenario(Employee)[0]

    Expected output

    ('ABC', 'ABC', 50000, 60000)
  2. Example 2

    Input

    scenario(Employee)[1]

    Expected output

    ('XYZ', 'XYZ')

+ 1 hidden test on Submit — assignment on an instance shadows.

Edge cases to ask about

  • Shadowing
  • Mutable class variables
How the tests call your code

These helpers run before your code. The test inputs above call them.

def scenario(cls):
    cls.company = "ABC"
    john, alice = cls("John", 50000), cls("Alice", 60000)
    before = (john.company, alice.company, john.salary, alice.salary)
    cls.rename_company("XYZ")
    after = (john.company, alice.company)
    john.company = "Solo"            # instance attribute shadows the class one
    shadow = (john.company, alice.company, cls.company)
    cls.company = "ABC"
    return before, after, shadow

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · Employee
    ⌘/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
    Class attribute + classmethodO(1)O(1)Instance lookup checks the instance dict, then the class.
    Walkthrough of the optimal approach (try it yourself first)

    Attributes in the class body live on the class and are shared; attributes set on self live on each instance. Reading john.company checks john's own __dict__ first, then the class.

    The trap: john.company = "Solo" does not change the class — it creates an instance attribute that shadows it for john only. And a mutable class variable (a list) is shared by everyone — usually a bug.

    Complexity: O(1) time, O(1) space. Attribute lookup is a constant number of dict checks.

    Reveal the reference solution
    class Employee:
        company = "ABC"
    
        def __init__(self, name, salary):
            self.name = name
            self.salary = salary
    
        @classmethod
        def rename_company(cls, new):
            cls.company = new

    Follow-ups interviewers ask

    • What goes wrong with items = [] as a class variable?
    • @classmethod vs @staticmethod?

    Frequently asked interview questions

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

    What is the time complexity of Class Variables vs Instance Variables in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. Attribute lookup is a constant number of dict checks.

    What follow-up questions do interviewers ask about Class Variables vs Instance Variables?

    What goes wrong with items = [] as a class variable? @classmethod vs @staticmethod?