Google L3 interview | Onsite Question

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2025-06-03Added By Anonymous
Source: Link

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 1<span> 2 / | \ 3 / | \ 4 / | \ 52<b> " is very" 3<i> 6 | 5 | 7 | | 8"This" " funny" 9 4 6 10Index Content Children (indices, ordered) 111 <span> 2, 5, 3 122 <b> 4 133 <i> 6 144 "This" 155 " is very" 166 " funny" 17

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.

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

Given the Node and function to complete

1public interface Node { 2 @Nullable String getText(); 3 Node[] getChildren(); 4 @Nullable Node getParent(); 5 boolean isText(); 6} 7 8public List<Node> findText(Node head, String search) { 9 // code goes here 10} 11
Comments