Python Interview Questions for beginners

Imagine that you are preparing to go to your Python interview and looking for the ultimate interview question that would show all your skills and knowledge.

How about the most simplest interview question that would suit to the beginner - swap values of two variables! You could obviously provide the following answer

temp = foo
foo = bar
bar = temp

unfortunately this in not a Python way of coding and the interviewee would be definitely not happy with your performance during this coding test. How about swapping the values without using a third variable? The answer below is much better, but it is still not a Python way to solve the problem. Now is better than never.

foo = foo + bar
bar = foo - bar
foo = foo - bar

The correct answer the interviewee is patently trying to get is

(foo, bar) = (bar, foo)

and this would be a great confirmation of your Python skills and knowledge, but of course the interview for beginner is not over yet. In the face of ambiguity, refuse the temptation to guess.

No comments:

Post a Comment