Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Trie Word Search on Grid

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Trie Word Search on Grid. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Meta wants to scan a character grid for valid keywords that may appear in surfaces like Messenger game boards. Given a 2D board of lowercase letters and a list of lowercase words, return all words that can be formed by traversing adjacent cells.

A word can be constructed from letters of sequentially adjacent cells, where adjacency is horizontal or vertical. The same cell may not be used more than once in a single word.

Formal Specification

Implement a function that takes:

  • board: a list of m rows, each row a list of n lowercase characters
  • words: a list of distinct lowercase strings

Return a list of all words from words that appear in the board. The output may be in any order.

Constraints

  • 1 <= board.length <= 12
  • 1 <= board[0].length <= 12
  • 1 <= words.length <= 3 * 10^4
  • 1 <= words[i].length <= 10
  • board[i][j] consists of lowercase English letters
  • words[i] consists of lowercase English letters
  • All words in words are unique

Function Signature

def find_words(board, words):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output