Problem · Matrix

Can Fit the Word

MediumGooglePHONE SCREEN
See Google hiring insights

Hi! The outputs are just placeholders. This problem is here to give you a glimpse of what a Google interviewer asked in one of their phone screens today. And today is 03-27-2025

We are given a grid of size N*M having characters "#", "" (empty space), and any English lowercase alphabet letter.

  • "#": Filled
  • "": Empty
  • Any English alphabet letter

We are also given a string word. We have to find if we can fit this word in the grid either by directly matching with English alphabet letters or using the empty spaces (by filling them with matching characters). The word should match exactly, and no empty space should be left before and after. We can have the word match from left to right and top to bottom only.

Examples
01 · Example 1
grid = [['#', '#', '#', '#'], ['L', '_', '#', ' '], ['A', 'L', 'A', '_'], ['#', 'X', '#', '_']]
word = "ALA"
return = 0

The word ALA does not fit as we have one space left at the end.

      #### ####
      L_# ALA_# 
      #X#__ #X#__
      
02 · Example 2
grid = [['#', '#', '#', '#'], ['L', '_', '#', ' '], ['A', 'L', 'A', 'N'], ['#', 'X', '#', '_']]
word = "ALAN"
return = 1

The word ALAN fits.

      #### ####
      L_# ALAN# 
      #X#__ #X#__
      
More Google problems
drafts saved locally
public int canFitTheWord(char[][] grid, String word) {
  // write your code here
}
grid[['#', '#', '#', '#'], ['L', '_', '#', ' '], ['A', 'L', 'A', '_'], ['#', 'X', '#', '_']]
word"ALA"
expected0
sign in to submit