r/programming • • Sep 22 '20

Google engineer breaks down the problems he uses when doing technical interviews. Lots of advice on algorithms and programming.

https://alexgolec.dev/google-interview-questions-deconstructed-the-knights-dialer/
6.4k Upvotes

1.1k comments sorted by

View all comments

Show parent comments

3

u/professor_jeffjeff Sep 23 '20

The only issue I have with this (I've done it) is that it's actually really surprisingly hard to intentionally write bad code, especially if you want it to be bad in a particular way.

1

u/serviscope_minor Sep 23 '20

yeah it is. It took quite a few iterations before we were happy with it and it was used, so it was very thoroughly removed.

Some of the patterns may have been uh inspired by production code...

1

u/professor_jeffjeff Sep 25 '20

I had to create specifically bad code for a class assignment on working with and refactoring legacy code. I tried and failed several times to get what I was really looking for. What ended up working was creating a list of features and implementing them rapidly with almost no testing in a somewhat random order, then creating a list of enhancements to those features that were typically exceptions to the "normal" flow of the code (e.g. ok, now there's a new damage type called "fire" that affects all monsters except skeletons) and then trying to revert a couple of those (e.g. skeletons are still immune to fire but now there's an anti-skeleton spell that makes weapons do double damage to skeletons but if the weapon also has the fire effect then it only does double the base damage and the fire damage is ignored).

it took me about 45 minutes to write an implementation and because of my absolute lack of refactoring and ignoring the test failures around the few things I did have, it ended up being a perfect single object that was probably a total of about 50 lines of code. Grading that assignment was difficult since there were so many possible solutions but generally students did a good job and actually said they had fun with the assignment.

The morale of the story is: To write intentionally bad code, first write simple code that is good, then try to maintain that code using bad development practices.