Wordle in the morning. Wordle in the evening. Wordle all the time.
Wordle
In reality, you can only play Wordle once a day. Anyway, I played Wordle the other day after I stopped playing for a while because I didn’t enjoy the time it took (I know, sad!). Now when I play, I try and solve it as quickly as possible, rather than in as few guesses as possible, so I don’t spend too much time on the game.
My new wordle strategy is neither here nor there for this post, but as I was playing, I was like “this wouldn’t be too hard to implement using Python terminal.” I was then like, “this would be a great in-room interview question.” As part of a technical assessment, you could give a candidate a laptop with their choice of programming languages installed, no internet, and have them build a simple implementation of Wordle. So, I decided to pretend it was just that and create Wordle in the terminal. First, I’ll explain why I think this is a good interview question and then present my implementation.
Why is this a good interview question?
I think there are a few reasons why. First, it can take place in a room and on a laptop. It isn’t too hard but is more complex than FizzBuzz (I previously wrote about that here). And while the logic is more advanced, it is not so difficult that it can’t be accomplished in 30 or so minutes.
You don’t need additional libraries outside Python’s standard libraries and a dictionary of five-letter words. I did have to look up the proper characters for yellow and green in the terminal (I would not expect anyone to know that), so I would probably hand the person the Colors class.
There is an element of controlling and updating the state of an object, which is quite common in programming. Finally, there are a few spots to show language understanding and patterns.
I was able to use a dunder method to pretty print the board with “__repr__”
I was able to use recursion when a guess didn’t meet the necessary criteria
And there are multiple spots to use for loops, string manipulation, and good naming conventions.
All three important parts of programming.
The Results
Winning
Losing
The code
The first class we make is a Colors class. This is to help us print the letters with color in the terminal.
Like so:
Next, we have the Board class. This keeps track of the state of the game and helps show the person playing the game the state of the board by pretty printing the board to show that colors properly show up.
Yellow for correct guess and wrong spot and green for correct letter and correct location.
Finally, the function to play the game.
What could I do better?
After implementing this, I would probably ask the interviewee what they could do better. So, I will go ahead and answer that.
I probably could’ve taken five minutes to write out requirements and 4 words that could cover most cases of the game to test playing with these words. This might’ve saved me from my mistake detailed below.
I probably don’t need to make a copy of the board just for printing. Could’ve manipulated how the board displayed before it prints. This would make it more memory efficient. (Although, it’s a tiny program and having two copies made it easier to write - for me)
Write some tests. With the extra time, you could potentially write some tests to check the logic. My test method was to play the game.
A mistake!
As I was writing this, I noticed a bug in my code that I had not even thought about until now: controlling for winning words with the same letter! Oops! Now, the game would still work, but the user would get very confused because the second instance of the letter would be yellow and not green. I’ll go ahead and update this function.
The line
self.winning_word.index(letter) == i:
needs to be some form of
check = [n for n, l in enumerate(self.winning_word) if i == n and l == letter]
if check:
We will blame whoever peer-reviewed the code for this mistake.* In all seriousness, I wouldn’t hold it against someone for making this same mistake, but it does show why laying out requirements ahead of time and code review is important.
Conclusion
I think this would be a tremendous in-room interview question to ask. There isn’t any need for outside resources, there is more advanced logic than fizzbuzz, and it is a fun exercise for the person implementing!
* That was me.