L2 · Working engineerObject-oriented Python~6 min · 4 tests#75

Abstract Base Class: Shape, Circle, Rectangle

Use Python's abc module to define an abstract Shape with area(), then implement Circle and Rectangle. Instantiating Shape must fail. Tested live.

The problem

  • Shape is an abstract base class with an abstract method area(). Shape() must raise TypeError.
  • Circle(radius) and Rectangle(width, height) implement area().

Examples

  1. Example 1

    Input

    round(Circle(2).area(), 4)

    Expected output

    12.5664
  2. Example 2

    Input

    Rectangle(3, 4).area()

    Expected output

    12

+ 2 hidden tests on Submit — abstract class cannot be created.

Edge cases to ask about

  • Instantiating the abstract class
  • Float rounding

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · Shape
    ⌘/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
    ABC + @abstractmethodO(1)O(1)Python refuses to instantiate a class with unimplemented abstract methods.
    Walkthrough of the optimal approach (try it yourself first)

    Inherit from ABC and decorate area with @abstractmethod. Python then refuses to instantiate Shape (or any subclass that does not implement area) with a TypeError. That turns a missing method from a runtime surprise into an immediate error.

    typing.Protocol is the structural ("duck-typed") alternative.

    Complexity: O(1) time, O(1) space. Each area() is a constant-time formula.

    Reveal the reference solution
    from abc import ABC, abstractmethod
    import math
    
    
    class Shape(ABC):
        @abstractmethod
        def area(self):
            ...
    
    
    class Circle(Shape):
        def __init__(self, radius):
            self.radius = radius
    
        def area(self):
            return math.pi * self.radius ** 2
    
    
    class Rectangle(Shape):
        def __init__(self, width, height):
            self.width = width
            self.height = height
    
        def area(self):
            return self.width * self.height

    Follow-ups interviewers ask

    • Add an abstract property perimeter.
    • ABC vs Protocol?

    Frequently asked interview questions

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

    What is the time complexity of Abstract Base Class: Shape, Circle, Rectangle in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. Each area() is a constant-time formula.

    What follow-up questions do interviewers ask about Abstract Base Class: Shape, Circle, Rectangle?

    Add an abstract property perimeter. ABC vs Protocol?