Objects You Can Call

Hard ⏱ 18 min 27% acceptance ★★★★★ 4.5
Implement CallCounter(limit) whose instances are callable: each call increments an internal count and returns 'ok' until limit is exceeded, after which it returns 'blocked'. __call__ turns objects into stateful functions — a common rate-limiter/middleware shape.

Examples

Example 1
Input
c = CallCounter(2); [c(), c(), c()]
Output
['ok', 'ok', 'blocked']
Explanation

The third call crossed the limit.

Constraints

  • Implement __call__.
  • State persists across calls.

Topics

OOP__call__

Companies

CloudflareAkamaiNetflix

Hints

Hint 1

def __call__(self): gives instances function syntax.

Hint 2

Compare the running count against the limit.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.