Google L3 interview | Onsite Question

Anonymous User
1983
Jun 03, 2025

Can someone help with the question:

Consider the following HTML:

<span><b>This</b> is very<i> funny</i></span>

Which forms the following DOM tree

       1<span>
     /    |     \
    /     |      \
   /      |       \
2<b>   " is very"   3<i>
 |        5          |
 |                   |
"This"            " funny"
 4                   6
Index Content	Children (indices, ordered)
1	   <span>	   2, 5, 3
2	   <b>	     4
3	   <i>	     6
4	   "This"	
5	  " is very"	
6	  " funny"	

In this problem, we are given the DOM tree and the string the user wishes to search for, and we want to return the list of consecutive text nodes that contain the search string.

So searching for
"hi" -> node 4
"his is very" -> nodes 4 and 5
"is very fun" -> 5 and 6
"is" -> only node 4 - we’ll return just the first set of nodes that contain our string.
"funny is very" -> 6 and 5
  

Given the Node and function to complete

public interface Node {
  @Nullable String getText();
  Node[] getChildren();
  @Nullable Node getParent();
  boolean isText();
}

public List<Node> findText(Node head, String search) {
  // code goes here
}
Comments (5)