All jobs / Automation developer
Automation developer interview question · 1–3 years
You need setup and guaranteed teardown around a block. What do you write?
The strongest answer
A context manager — @contextlib.contextmanager with a yield between setup and cleanup in a finally, or a class with __enter__ and __exit__
Why it works
Exactly, and the finally around the yield is what makes it hold on the error path.
What a good answer covers
- with guarantees cleanup on every exit.
- @contextmanager: setup, yield, cleanup in finally.
- Or __enter__ / __exit__ on a class.
- __exit__ can suppress an exception — usually it should not.
Answers that fall short
- Close, but not the strongest: A try/finally at every call site
That works but repeats the cleanup everywhere, which is what it is for. - Weak: A decorator, which is the same thing
A decorator wraps a function; a context manager wraps a block. - Red flag: A pair of setup() and teardown() functions and a convention that callers call both
A convention is not a guarantee.
Who gets asked this
- Backend developer
- Full-stack developer
- Automation developer
- DevOps / SRE / cloud
- Data analyst / BI
- Data engineer
- Data science / ML / AI
- AI engineer
More Automation developer questions
- Can you rely on a dict keeping insertion order?
- One synchronous database call inside an async handler makes the whole service slow. Why?
- Three independent API calls run one after another in an async function. How do you overlap them?
- How do you compose several transformation stages over a huge stream without buffering?
- A Python service does heavy numeric work per request and latency is unacceptable. What options do you weigh?
- Under load your async service exhausts database connections. What went wrong?
- What is a decorator, really?