Ctrl K
Python · Mid-level · Theory

A service has a function def add_tag(tag, tags=[]), and after a few calls, the results start containing tags that belong to other calls. What's going on, and how do you fix it?

~4 min by voice

How to approach it

The interviewer wants to see whether you understand how Python evaluates default values and the difference between mutating an object and rebinding a name, not just whether you know the "None recipe".

Have you seen this question in a real interview?
Answers0
Avg. score—
Strong—
Practiced0
Topics
Similar questionsImagine a function add with a parameter items whose default value is an empty list. The function appends an element to that list and returns it. What happens if you call it twice in a row without passing the second argument? JuniorTell me how mutable data types in Python differ from immutable ones. Give examples and explain why it matters in practice. JuniorWhat happens when you override __eq__ in your class, and how does that relate to __hash__? How would you make an object that can safely be put in a set and used as a dictionary key? SeniorWhat are magic, or dunder, methods in Python? What are they for? Give a few examples. JuniorWhat's the difference between a shallow copy and a deep copy in Python? When does that difference matter? Junior

Answer by voice

The interviewer will ask this question and listen. Think out loud and ask for a hint if you need one. When you're done, pause or tap “That's my answer”. In about a minute, your score and the points you covered show up here.

Sign in to answer

Best with headphones · ~4 min