Hackerrank Crossword Puzzle Javascript Solution

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!


Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store