All jobs / Automation developer
Automation developer interview question · 1–3 years
Can you rely on a dict keeping insertion order?
The strongest answer
Yes since Python 3.7 it is part of the language spec — but if the order is meaningful to the reader, say so, and use OrderedDict when you need order-sensitive equality or move_to_end
Why it works
Right, and the caveat about equality is the part that still matters.
What a good answer covers
- Insertion order is guaranteed from 3.7.
- Sets are still unordered.
- OrderedDict for order-sensitive equality and move_to_end.
- Say it in the code when order carries meaning.
Answers that fall short
- Close, but not the strongest: No, dicts are unordered by definition
That was true up to 3.6, where it became an implementation detail. - Weak: Only if you sort the keys first
Sorting gives sorted order, not insertion order. - Red flag: Only for dicts with string keys
Key type is irrelevant.
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
- 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?
- You need setup and guaranteed teardown around a block. What do you write?