Hackerrank Crossword Puzzle Javascript Solution

Eyüp Ferhat Güdücü

Last week I come across a code challenge at Hackerrank

This is a problem about putting the keywords at the available zones on a crossword puzzle for both horizontal and vertical directions.

Here is the code!

This was fun to solve it. Let me try to explain step by step with the function names on code;

  1. First of all, we need a function to find all the available zones. This is a little bit tricky because of the vertical direction. findSpaces()
  2. We need a function to determine if the zone is fitting with the keyword. isValidSpace()
  3. We‘ll do backtracking. So we need forward() and backward() functions. forward() will put the keyword on the corresponding place before recursion, backward() will undo it after recursion.
  4. Lastly, we need a recursive function named solve(). This is the wedding part. It will find the possible spaces by length and try to put the words on zones recursively one by one. If it fails it’ll do a rollback. Usual backtracking.

Happy coding!


Eyüp Ferhat Güdücü

Written by

Mostly developer, almost traveller, already gamer http://eferhatg.com/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade