I guess I have a lot of archives. This question is mainly about reasoning-by-cases, which is a way of handling a question that isn’t explicitly called out so well by the usual exercise batteries, in my experience (it’s neither a data structure nor an algorithm, not really).
class Solution:
def judgeCircle(self, moves: str) -> bool:
x = 0
y = 0
for m in moves:
if m == 'U': y += 1
if m == 'D': y -= 1
if m == 'R': x += 1
if m == 'L': x -= 1
return x == 0 and y == 0
Discussion
- The question is of course contrived, but I think it’s a good exercise in initial practice in “understanding a question” and translating that to a programming-amenable approach.
- The question does invite overthinking it, I would guess. Hopefully not everyone, but I can see a student panicking and starting to make an array-of-arrays. Knowing that sometimes you don’t have to do the hardest thing you can think of is very valuable in interviews and in practice!
I don’t know really know a general category to put this in. I’ll call it an introductory interview question, and collect any more here.