Solving My Google Phone Interview Question

Keep On Coding
26 Nov 201912:42

Summary

TLDRIn this video, the creator walks through the Google phone interview process, specifically focusing on a coding problem involving trees and depth-first search. The video explains how candidates are required to write code in a Google Doc without IDE support, encouraging proficiency in the coding language. The example problem involves printing a tree structure from a list of parent-child relationships. The solution utilizes a HashMap for depth-first traversal and discusses the time and space complexity of the approach. The video also provides tips for tackling coding interviews, and encourages viewers to practice in similar environments.

Takeaways

  • ๐Ÿ“ฑ The video discusses the Google phone interview process and coding challenges.
  • ๐Ÿ”— A link to the full Google interview experience and Amazon phone interview is provided in the description.
  • ๐Ÿ“ During the Google phone interview, candidates code in a Google Doc without code completion or syntax error checking.
  • ๐Ÿ’ก It is recommended to practice writing code in Google Docs before the interview to get familiar with coding without an IDE.
  • ๐ŸŒณ The coding prompt provided involves writing a Java function to print a tree based on a list of parent-child relationships.
  • ๐Ÿ” The solution involves using a depth-first search (DFS) approach to navigate the tree structure.
  • ๐Ÿ“„ An adjacency list (represented using a HashMap) is used to store the tree relationships, where each parent points to its children.
  • ๐Ÿง  To find the root of the tree, the candidate can create a set of all children and loop through the keys to identify the root node.
  • ๐Ÿงฎ The time complexity of the solution is O(n), where n is the number of nodes in the tree, and the space complexity is also O(n).
  • โœ… The provided solution passed the interview, allowing the candidate to advance to the on-site interview round.

Q & A

  • What is the primary topic discussed in the video?

    -The primary topic discussed is the Google phone interview, including how to prepare for it and solve a specific coding problem given during the interview.

  • What is the first tip provided for preparing for the Google phone interview?

    -The first tip is to practice writing code in a Google Doc without code completion or syntax error checking, as the interview environment does not provide these features.

  • What is the coding problem presented in the Google interview example?

    -The coding problem involves writing a Java function called `printTree` that prints a given tree to standard output based on a list of parent-child relationships.

  • What data structure is recommended for solving the problem in the video?

    -The video suggests using an adjacency list, which is represented by a HashMap, where each parent node is a key and the corresponding value is a list of its children.

  • What approach is used to traverse the tree in the given solution?

    -The video uses a Depth-First Search (DFS) approach to traverse the tree, printing each node along the path and keeping track of the level to format the output correctly.

  • How is the root of the tree determined in the solution?

    -The root of the tree is determined by looping through all the parent nodes and checking which one is not a child in the relations. This node is the root since it has no parent.

  • What challenge did the presenter face when implementing the solution?

    -The presenter mentions that the biggest challenge was determining the root of the tree, as an extra set was required to store all the child nodes.

  • What is the time complexity of the solution provided?

    -The time complexity is O(n), where n is the number of nodes in the tree. The algorithm loops through the nodes multiple times, but constants are dropped in Big-O notation.

  • What is the space complexity of the solution provided?

    -The space complexity is also O(n) since the algorithm uses extra space for the adjacency list and the set of child nodes, which both can store up to n elements.

  • What advice is given for candidates who donโ€™t understand the problem during the interview?

    -The video advises candidates to ask questions during the interview to ensure they fully understand the problem before trying to solve it.

Outlines

00:00

๐Ÿ“ฑ Google Phone Interview Overview and Coding Tips

The video begins with the host thanking the audience for watching and introduces the topic: the Google phone interview process. It references other videos about interview experiences at Google and Amazon. The Google phone interview requires coding in a Google Doc, without code completion or syntax error checking, making it challenging for those accustomed to IDEs. The video suggests practicing in a Google Doc beforehand to ensure familiarity with a language. The plan for the video is explained, which includes discussing the interview prompt, outlining an algorithm, writing code, and analyzing the solution.

05:00

๐Ÿง  Understanding the Google Interview Problem Prompt

The speaker introduces the coding problem from the Google interview: to write a Java function `printTree` that prints a tree structure based on a stream of parent-child relationships. An example set of relations is provided where animals like 'mammal' and 'bird' are siblings under the 'animal' node, and 'animal' is a child of 'life-form'. The function must print the tree in a specific format using a depth-first search. The problem is analyzed as a tree traversal challenge, where nodes like 'bird' and 'fish' are printed in a structured manner as children of 'animal'.

10:02

๐Ÿ“ Steps to Solve the Tree Printing Problem

The speaker explains the process of solving the tree-printing problem. The first step is to convert the list of relations into a data structure, such as a HashMap, to allow depth-first search (DFS) traversal. The relations are treated like an adjacency list representing a graph. Next, the depth is tracked to ensure proper indentation (tabs) when printing child nodes. Since the root node is not explicitly provided, a method to find it is described by identifying a node that is not a child of any other node. The algorithm's high-level structure is explained with each step outlined.

๐Ÿ” Implementing the Tree Structure and Depth-First Search

The speaker walks through the code implementation. First, a HashMap is created to represent the adjacency list of the tree, and a set is used to track child nodes. The speaker explains how to loop through relations to fill the map with parent-child relationships. Then, by looping through the map keys and checking if they are in the child set, the root node is identified. A depth-first search (DFS) is performed, recursively visiting each child and printing the appropriate number of tabs based on the depth level. The DFS ensures the tree is printed in the correct format.

๐Ÿพ Testing and Analyzing the Code

After implementing the solution, the code is tested, and some adjustments are made to ensure the output matches the expected format. A minor issue with printing is corrected by changing a newline statement. The correct tree structure is printed, displaying 'life-form', 'animal', 'mammal', and their respective children. The time and space complexity of the solution is analyzed, both of which are O(n), where n is the number of nodes in the tree. The speaker reflects on the solution, acknowledging that the extra data structure for finding the root might be unnecessary but was sufficient to pass the interview.

Mindmap

Keywords

๐Ÿ’กGoogle phone interview

A Google phone interview is a remote interview process conducted by Google to assess potential candidates' technical skills and problem-solving abilities. In the video, the speaker discusses their experience with the Google phone interview, emphasizing the importance of being able to write code without the aid of an IDE, which is a common feature in such interviews.

๐Ÿ’กGoogle Doc

Google Docs is a free web-based office suite that allows for collaborative editing of documents in real-time. In the context of the video, the speaker mentions that candidates are expected to write code in a Google Doc during the interview, simulating the experience of coding without the benefits of an integrated development environment (IDE).

๐Ÿ’กProficiency

Proficiency refers to the state of being highly skilled or competent in a particular area. The video emphasizes that candidates should be proficient in the programming language they claim to know, as they will be expected to write code without code completion or syntax error checking.

๐Ÿ’กDepth-first search

Depth-first search (DFS) is a traversal algorithm used in graph theory and computer science. It starts at a given node and explores as far as possible along each branch before backtracking. In the video, the speaker explains that the problem they were given during the interview required a depth-first search approach to print out a tree structure.

๐Ÿ’กAdjacency list

An adjacency list is a collection of lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph. The video script describes using an adjacency list to represent the tree structure for the interview problem, where each parent node points to a list of its children.

๐Ÿ’กIDE

An Integrated Development Environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. The video discusses the challenge of coding without the assistance of an IDE during phone interviews, which is a skill that candidates are expected to have.

๐Ÿ’กSiblings

In the context of the video, siblings refer to nodes in a tree that share the same parent. The speaker mentions that the order of the pairs in the input list determines the nodes' order with respect to its siblings, which is an important detail in how the tree is printed out.

๐Ÿ’กTime complexity

Time complexity in computer science is the computational complexity that describes the amount of computer time taken by an algorithm. The video script includes an analysis of the time complexity of the solution provided, which is O(n), where n represents the number of nodes in the tree.

๐Ÿ’กSpace complexity

Space complexity is the amount of memory space required by an algorithm. The video script discusses the space complexity of the solution, which is also O(n), accounting for the storage of the adjacency list and the children set used in the algorithm.

๐Ÿ’กRecursion

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. The depth-first search algorithm described in the video uses recursion to traverse the tree, printing out nodes and then recursively printing their children.

๐Ÿ’กRoot

The root of a tree is the topmost node in a tree data structure from which all other nodes are descendants. The video script describes how to find the root of the tree, which is essential for starting the depth-first search. The root is identified as a node that is not a child of any other node.

Highlights

The Google phone interview requires coding in a Google Doc, without the support of code completion or error checking, making it important for candidates to be proficient in their chosen language.

The interviewer expects candidates to be able to write code in a plain text environment, showcasing their mastery of coding syntax and structure without an IDE.

Candidates should practice coding in a Google Doc to prepare for the interview environment, as this format is designed to test deep familiarity with the programming language.

The core problem presented in the interview is to build a tree structure from a series of parent-child relationships and print it in a hierarchical format using a depth-first search algorithm.

The interviewer provides a list of unordered parent-child relationships, and the candidate must organize them into a tree structure.

The key challenge of the problem is identifying the root of the tree, which requires determining which node does not appear as a child in any of the relationships.

Depth-first search (DFS) is employed to traverse the tree and print the nodes in the required hierarchical order.

Using a HashMap (adjacency list) to store the parent-child relationships is suggested as an efficient approach for building the tree structure.

The algorithm needs to keep track of the current depth in the tree to ensure proper indentation when printing the hierarchy.

A set is used to store all children nodes, which helps identify the root of the tree by finding a node that does not appear in this set.

The time complexity of the solution is O(n), where n is the number of nodes, as each node is visited once during the depth-first search and while building the map.

The space complexity is also O(n) due to the storage of the adjacency list and the children set.

DFS ensures that the tree is printed in the correct order, with each child printed beneath its parent node and proper indentation to represent depth.

The presenter emphasizes the importance of asking clarifying questions during the interview if the problem statement is not fully understood.

This solution was successful and led to the presenter advancing to the on-site interview stage at Google.

Transcripts

play00:00

good day subscribers thank you guys so

play00:01

much for watching in this video we're

play00:03

going to be talking about the Google

play00:04

phone interview now if you haven't

play00:06

checked it out already I do have a video

play00:08

talking about the entire Google

play00:09

interview experience so I will leave a

play00:11

link to that in the description also I

play00:13

have a video talking about the Amazon

play00:14

phone interview so you guys should

play00:16

definitely check that out after you

play00:17

watch this now in the Google find review

play00:19

they will give you the prompt and that

play00:20

will give you a Google Doc to write your

play00:22

coding I mean you can't build and run

play00:24

your code you don't have any code

play00:25

completion you don't have any syntax

play00:27

error checking so it definitely makes

play00:29

things harder if you are used to coding

play00:30

in an IDE that's why I recommend if you

play00:33

are prepping for this interview you

play00:34

should definitely practice writing code

play00:35

in a Google Doc and they do this to

play00:37

ensure that the candidate has a lot of

play00:39

experience in the language that they are

play00:41

coding in if you really are proficient

play00:42

in a language you should be able to code

play00:44

in it without needing any type of code

play00:46

completion you should know the correct

play00:48

spacing and where the brackets should go

play00:50

etc so I think it's a good thing so how

play00:52

the format of this video is going to go

play00:54

is I'm going to let you guys know what

play00:55

the prompt was we're gonna talk about a

play00:57

high level algorithm and then we're

play00:59

actually gonna write the code and then

play01:00

we're gonna do some analysis on it

play01:01

afterwards if you guys are enjoying the

play01:03

content please go ahead and hit that

play01:04

like button and please subscribe to the

play01:06

channel if you haven't and with that

play01:07

being said let's go ahead and take a

play01:08

look at what the given problem was all

play01:10

right so this is the actual prompt that

play01:12

was given during the interview so the

play01:14

objective is to write a Java function

play01:17

print tree which prints a given tree to

play01:19

STD out details the argument of print

play01:22

tree is a stream of relations pairs of

play01:24

parent-child relationships each string

play01:27

found anywhere in the input represents a

play01:29

unique node each parent can have many

play01:31

children the input list may contain

play01:33

relations in any order although the

play01:36

order in which the pairs appear in the

play01:39

input list determines the nodes order

play01:41

with respect to its siblings so if we

play01:44

come down here and look at an example

play01:45

input we see that we're given a list of

play01:48

relations and then down here we add six

play01:52

relations

play01:53

the first one is animal is a parent of

play01:55

mammal animal is a parent of bird so

play01:58

mammal and bird are siblings life form

play02:01

is a parent of animal so now we have

play02:03

life form points to animal points to

play02:06

mammal and bird cat as a parent of Lion

play02:08

mammal is a parent of

play02:10

an animal is a parent of fish and down

play02:12

here we call this print tree which is a

play02:15

static method of the tree printer class

play02:17

and it takes in our input as it's

play02:18

argument if we look at our output we see

play02:21

that life-form is printed first so this

play02:23

is going to be the root of our tree then

play02:26

down here notice we have this extra

play02:27

space here and then we have animal which

play02:29

is a child of life-form and its child is

play02:33

mammal its child is cat and cats child

play02:36

is lion also down here we have bird and

play02:38

fish notice that they're on the same

play02:40

level as mammal meaning that these three

play02:43

mammal bird and fish are children of

play02:45

animal and finally if we go down to the

play02:47

code that was provided we see that we

play02:49

have that class tree printer we have

play02:51

that method print tree which takes in

play02:53

the list of relations and this right

play02:55

here is where we have to write our code

play02:56

and down here we have that relation

play02:58

class which is a parent a child and a

play03:01

constructor now if we go back and look

play03:04

at the expected output notice how it's

play03:07

printed out so we have the route here

play03:09

first and then we have the child here

play03:11

and then we have the child of animal

play03:14

here so if you are familiar with graphs

play03:16

and trees you recognize a pattern here

play03:18

that this is actually going to be a

play03:19

depth-first search basically we're going

play03:21

down a certain path here down till we

play03:24

get to a node that doesn't have any

play03:26

children and then we go back up to the

play03:28

next highest sibling so once we're done

play03:30

with this path we see that we go back to

play03:33

animal and we print out its next child

play03:35

which is bird bird doesn't have any

play03:36

children so then you would go to the

play03:38

next child and you print out fish now if

play03:40

this problem didn't make any sense

play03:41

please make sure you go back and

play03:42

re-watch it if you are in a real

play03:44

interview and you don't understand the

play03:45

problem make sure you ask questions and

play03:47

make sure you completely understand the

play03:49

problem before trying to solve it all

play03:50

right so let's go ahead and move over to

play03:51

the editor and see if we can come up

play03:54

with a high level solution all right so

play03:55

here we have our tree printer class with

play03:57

the method print tree I went ahead and

play03:59

moved the relation class to its own file

play04:02

so it's called relation Java and has

play04:04

that same parent-child relationship so

play04:07

the first thing we want to do is we want

play04:08

to figure out the steps that we're gonna

play04:10

take to solve this problem so the first

play04:12

step we want to do is we want to convert

play04:15

our relations into some kind of data

play04:17

structure where we can do a depth first

play04:19

search on it so one thing we can use is

play04:21

we can use a hash map or every pair

play04:23

is the key and we can have its value be

play04:26

the list of children so for example in

play04:29

the example that was given we had

play04:31

something like life-form and that's

play04:34

going to point to a list of its children

play04:36

which would be in our case just animal

play04:38

an animal would be its own key pointing

play04:43

to its children so in our case we would

play04:46

have mammal bird and fish then down here

play04:49

we had mammal which pointed to cat so

play04:54

just given these three here what we

play04:56

could do is we could say okay look at

play04:57

light form give me give me all its

play05:00

children okay we have animal now let's

play05:02

do a search on animal okay we go to

play05:04

animal get all its children now we'll go

play05:06

to mammal look at its children and then

play05:08

now we have cat once we're done we'll go

play05:10

back and look at the next child of

play05:11

animal okay bird well bird doesn't have

play05:13

any children because it's not is it's

play05:15

not a key in our map next we look at

play05:17

fish fish also doesn't have any children

play05:19

because it's not a key in our map now

play05:22

some of you guys might recognize this

play05:23

but this is actually called an adjacency

play05:25

list an adjacency list is just one way

play05:29

of representing a graph or a tree

play05:31

because every tree is a graph right all

play05:33

right so that's gonna be the first step

play05:35

and when you're interviewing you want to

play05:36

actually like explicitly write this up

play05:38

so we could say something like loop

play05:39

through relations and put all parents as

play05:41

keys pointing to their children the next

play05:43

step we want to do is perform a

play05:45

depth-first search so we can simply say

play05:47

perform depth-first search with starting

play05:52

at root now one thing we want to notice

play05:55

is if we go and we look at the expected

play05:57

output again we do have a tab between

play06:00

every parent and its child so we need to

play06:03

keep track of which depth we're on and

play06:05

based on the depth that's how many tabs

play06:07

we're gonna have so if we go down here

play06:10

perform def first search starting at

play06:12

root we can also add perform depth first

play06:14

search starting at root keep track of

play06:16

depth to know how many tabs we need to

play06:18

write and this doesn't have to be super

play06:20

example or anything this is kind of more

play06:22

like just just this the like a

play06:23

high-level steps of what you're gonna do

play06:25

now one thing that we're forgetting is

play06:26

we need a way to actually get the root

play06:28

right because we were given this list

play06:31

here and I mean it's just the it's just

play06:33

a list of pairs and it doesn't

play06:35

explicitly tell us what

play06:37

route is so the easiest way to get the

play06:39

route is to in our first step we could

play06:42

put each child in in a set we could put

play06:48

each child in a set then we could have

play06:51

this as our second step and make this

play06:53

our third so what we can do is we can

play06:55

loop through the keys of our map once we

play06:57

find a key that's not in the children

play06:59

set we know that that's gonna be our

play07:01

route since it's not a child of anything

play07:03

and this will make more sense when we

play07:04

actually look at the code so let's go

play07:06

ahead and do that next all right so the

play07:07

first thing we need is gonna be our map

play07:09

here which is going to be our adjacency

play07:11

list that represents our graph we have

play07:13

this pointer here which is whenever

play07:15

we're going to put a new list of

play07:17

children into our map as a value and

play07:20

then we have our children set here which

play07:21

is going to show anything that's a child

play07:23

in this tree so the first thing we're

play07:25

going to do is we're going to loop

play07:26

through all the relations and we're

play07:27

gonna put him inside our map okay so

play07:29

here we're looping through each relation

play07:31

if the map already has this parent we

play07:34

want to go ahead and grab its list of

play07:36

children and we want to add whatever our

play07:38

current relation is and we're gonna

play07:40

specifically add the child otherwise if

play07:43

we haven't seen the key yet in her map

play07:45

we're gonna go ahead and create a new

play07:47

list we're gonna put the child in that

play07:49

list and we're gonna put a new entry in

play07:51

our map with the parent as the key and

play07:53

the list as the value finally the last

play07:57

thing we need to do is we need to add to

play07:59

our children set and we're just going to

play08:03

add the child of the current relation

play08:05

we're on and since it's a set it won't

play08:07

add any duplicates the next thing we

play08:10

need to do is we need to find our route

play08:11

so we're gonna go ahead and loop through

play08:13

all the keys in our map and we're gonna

play08:15

check if it's in the children set so

play08:18

down here we have this for loop here and

play08:20

we're just gonna basically check in this

play08:22

if statement if the children set

play08:25

contains the current key that we're on

play08:28

in the map rather if it doesn't contain

play08:30

it then we know that this is our route

play08:32

and we're just gonna set root equal to

play08:35

entry get key and we're gonna break out

play08:37

of this for loop and at this point we

play08:40

know what our route is and we have our

play08:42

map so now we just need to perform the

play08:44

depth-first search so down here we can

play08:46

simply just call DFS which we haven't

play08:48

created yet we're gonna pass in our

play08:49

route

play08:50

you know passing the depth right because

play08:52

we need to know how many tabs to print

play08:53

and then we also need to pass it in our

play08:56

map all right so this is what our

play08:57

depth-first search is going to look like

play08:59

it's going to take in the string which

play09:01

is the root the level that we're on and

play09:03

the map the first thing we're gonna do

play09:05

is we're going to print out as many tabs

play09:06

as the level that we're on

play09:12

once we do that we're going to print out

play09:14

the route

play09:17

we need to get all the children of that

play09:18

room

play09:25

you

play09:27

so the first thing we're going to do is

play09:29

we need to check if the map actually

play09:30

contains that key if we're ever on a

play09:32

leaf of the graph it won't have anything

play09:35

as a key in the map so we need to check

play09:38

against that finally once we do get the

play09:40

children we need to just loop through it

play09:42

and recurse

play09:49

and as we are rehearsing we need to

play09:51

increase the level by one so when we

play09:53

print it out we're printing out an

play09:55

additional tab at each level all right

play09:58

so let's go ahead and hop back over to

play09:59

Maine and what we can actually do is we

play10:01

can go ahead and run this and down here

play10:06

we see that it doesn't look right and

play10:09

that's because if we go back to the tree

play10:11

printer this right here is actually

play10:14

going to be print instead of printing a

play10:16

new line so if we go back and we go

play10:19

ahead and run that we see that we do

play10:25

actually get what is expected so we have

play10:29

life-form animal mammal cat fly and

play10:32

burden fish and up here we see that that

play10:35

is the expected output so what is the

play10:39

time and space complexity of this

play10:40

algorithm well if we want to say that

play10:42

the number of nodes in our tree is n we

play10:45

are doing a four loop through all the

play10:47

relations which could potentially be n

play10:49

elements so that's gonna be n we do

play10:52

another for loop through our map which

play10:54

is also n and finally we do this

play10:56

depth-first search which could

play10:57

potentially be n as well so so our time

play11:04

complexity here is going to be Big O of

play11:07

technically we are looping three times

play11:09

through the elements so it's going to be

play11:11

three n but since we drop constants it's

play11:14

just going to be Big O of n for the

play11:16

space complexity we are creating this

play11:18

map and storing all the relations in

play11:20

that map so that's gonna be N we also

play11:23

have this children set which is also

play11:25

going to store n up to n elements we

play11:29

also have this depth-first search here

play11:30

which could potentially be called for

play11:32

every element so again the space

play11:34

complexity would be Big O of 3n which if

play11:38

we drop the constant will just be n so

play11:41

this is the solution that I came up with

play11:43

during the interview the thing that I

play11:45

was caught up with was them with the

play11:47

most was the finding the root because we

play11:51

are creating this extra child set this

play11:53

children set here in fact and it seems

play11:56

like this is extra data that could be

play11:58

used but I haven't been able to think of

play12:01

a better solution for this

play12:02

if you

play12:03

I can think of something please leave it

play12:04

down in the comments but other than that

play12:06

that's gonna wrap it up for this

play12:07

question this solution that I wrote up

play12:09

ended up passing me through to the

play12:11

on-site interview so obviously in their

play12:13

eyes it was an acceptable solution so I

play12:15

just wanted to share that with you guys

play12:16

but yeah other than that that's gonna

play12:18

wrap it up for this video if you guys

play12:20

have any questions or comments please

play12:22

leave them down below in the comment

play12:24

section for anyone who is in your being

play12:25

at Google good luck I hope this video

play12:28

was helpful and yeah until next time I

play12:30

will see you guys in the next video

play12:32

[Music]

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Google interviewphone interviewcoding tipstree traversaldepth-first searchJava functionalgorithm practicecoding prepprogramming interviewtech interviews