FRANK VAN PUFFELEN: Hey, I'm Puf.
I'm a Firebase engineer, and I'm here for my interview.
[MUSIC PLAYING]
I've been a Firebase engineer for eight and 1/2 years now,
almost nine, and I've been using Firebase for 12 years.
I've had great interviews and not-so-great interviews,
I would say.
It really depends.
Coding questions are always challenging,
though, I find, because you could just
go off on the completely wrong direction in your answer.
And that's the hardest, and especially
when you realize that you're on the wrong track,
because then you're just trying to fix it.
So yeah, I'm curious how that's going to go today.
So back when I did my interviews at Google,
one of the questions I got is, what is Firebase?
And that was so interesting because Firebase had just
been acquired.
And I was interviewing to be the explainer.
So I literally explained a bit about our backend domain
product, the Realtime Database, and how
it's like storing a JSON tree in the cloud,
and how you then can access every individual value
through either the SDK or a unique URL.
And, yeah, my interviewer seemed to like that question
because here I am.
Best advice is to speak out loud as you're solving
the problem in your head.
So when you're thinking of what to do,
say the thing that you're thinking,
because that gives your interviewer the best context
of what you're thinking of.
It also gives them the chance to correct you
if you're not heading in the direction
that they were thinking of.
Many people cram for their interviews at Google,
and I don't think that's the best idea.
You need to know your stuff, but you can't really
cram for that, right?
It's not an exam.
And that also makes them focus on things
like the algorithm name, which isn't really the point, right?
We're not testing you for a specific algorithm.
We're testing you for, like, general cognitive ability.
Are you smart?
Do you know the thing?
But, of course, you need to things like speed
versus time tradeoffs.
But you don't need to know the exact QuickSort variance
that you need to implement.
[MUSIC PLAYING]
It's a fun question.
Let's give it a try.
This question is about time.
"Given two times in 24-hour format,
one with missing digits that are marked as a question mark
and the other one with no missing digits,
we want to replace all of the question marks in the first time
by digits 0 to 9 in such a way to obtain the time
that's closest to the second time."
So "the earliest possible time is 00:00,
and the latest valid time is 23:59.
Time X is closer to Y than to Z if the difference between X
and Y is smaller than the difference between Y and Z."
So let's say that we have a time where we want to meet--
it's all going to be a string.
So let's say that we want to meet 23:57.
So this is the second--
the target time that we want to reach.
And, for example, let's say that we have something like--
let me think--
23-- that's a 2, a question mark, colon,
question mark, and then an 8.
So the closest we can get here, just from thinking, is.
23:58.
How do we do that in codes?
First thing I would do is probably get rid of the string
and just convert everything to minutes.
So I would need a function for that.
But let's just assume that we have it
in just a minute since midnight.
So let's say inMinutes or something as the function name.
So we get the time.
Then I would probably just write "time" as a string.
I would split it on the colon because that's always
going to be there.
I'm going to use s for now just because I
didn't leave enough space.
OK.
So then we do--
and I'm using only this time, the target time, for now.
So then we do--
let's see.
We have s0.
We do parseInt() on that.
This is JavaScript.
So I'm converting the number of hours to minutes for this one.
And since that is hours, we do it times 60.
Then we add parseInt split 1, 1.
OK.
So that's minutes.
So return that.
OK.
So if I call m being in minutes 23:57, then m will be--
23 times 60 plus 57 is 1,450--
1,437 or something like that.
OK.
Hmm.
I'm not sure how I get from this to that in that case.
The simple version is actually just parsing the string
character by character and then, if it's a question mark, just
finding--
well, essentially filling in this one.
But the problem we're going to run into is if the hour reps.
Right, so let's say that the target time is 9:00 AM--
so 09:00.
And then we fill something in that's before that.
So let's say-- let's just do one variable now, like,
08:4 question mark, something like that.
Oh, got it.
This actually helps.
Because the closest I can get here is going to be 08:49
because that's the highest digit we have.
And why do I put a 9 here?
It's because it's after now.
Got it.
So is that what I need to do?
Hold on.
Let's see.
So we're counting from the start,
and here the 2's are the same.
So once I get to this first question mark,
I fill in the same value.
That makes sense.
And here I fill in the same value.
That makes sense.
So what do I do here?
I start on the 0.
Then I have an 8 versus a 9.
So I now know that my target time is after the current time.
So that means that any question marks after this,
I need to fill in 9's--
well, the highest number possible.
If this would be a question mark, for example--
hold on.
Let's actually do that one, too.
Then-- sorry about that.
It's really unreadable.
That was an 8.
So 08, question mark, question mark--
then the closest we can get to that 9 is going to be 08:59.
And we know that because the moment
we get to the second character, the 8 is lower than the 9.
Here I can do the entire split.
But here I can't, because there's a question mark in here
already.
So let's do it digit by digit.
Let's see if we can write a function for this.
We don't need the parsing at all, it turns out.
But what we do need to do is we need
to start looping over that second string.
Let's say that we have--
what did we say?
There was the target string, which is this one.
And I'm going to actually use this one first.
Let's see that-- this one is my t variable.
And this one is my--
I don't know, source--
s variable.
So we're going to loop over the characters
in the string in JavaScript.
Let's do that by splitting the s string
and splitting the t string.
In fact-- yeah, OK.
Let's do it like this. t is t.split,
which is just a way to--
we do the same for s.
So this gives us just the array of characters.
Then we need to handle character 0, 1, 2, and 4.
So we're going to loop over that for--
I'm going to ignore all the lets and things like that.
But for i is 0--
so while i less than 5, and i is not equal to 2.
So we're skipping the colon.
And we do i++.
OK.
And we need a variable to determine
if we're still the same, so in the 2 part here,
or if we are greater or lower.
So I'm going to do a variable for that.
Let's call it delta or something, d.
And we're going to start it on 0.
0 is there the same.
Minus 1 is we're before the target n.
Plus 1 is we're after the target, or the other way around.
We'll figure that out.
So first thing we can do is we can only modify the question
marks anyway.
So that's fine.
And we're building the output string,
if we can do that by modifying s.
So that's also good.
So the only thing we can modify is question marks.
We first need to update our d.
So if-- what did I say?
s i is not a question mark, then we need
to compare it to something.
And we're still d equals 0.
Then we need to do that.
So not sure if a string comparison works,
but I'm going to pretend that I can compare these s strings just
because it's slightly easier.
It's less code, essentially.
So we may need to convert the strings to numbers.
But otherwise, let's see.
What did I say?
Then we're on the question mark.
If the target i is greater than the source i then d
becomes plus 1.
Otherwise, if target i is less then the source's i,
then d is minus 1.
So it's an absolute set, not an increment or decrement.
OK.
OK, now we know where we are.
Then for a question mark, we do--
let's see.
So if s i is a question mark, then we can replace it.
So if d is 0, then we can replace it
with the value from the target.
And we're going to have to go down here a bit.
If d is minus 1, then we set it to--
hold on.
Let's see.
If i-- wow, It's a lot of ifs.
Sorry about that.
If i is 0, then s i--
hold on.
So minus 1 means that we're here.
So the target is before now?
Hold on.
Let me quickly check which one we need there.
So we need the 0 there, always.
That's actually-- oh, this applies to all of them,
I realize, because all of them, the minimum value is 0.
So we can just do this.
The tricky bit is if d is plus 1,
like 1, then we need to have a different number for each
of them because for the--
don't know, the large hours, the maximum value it can have is 2.
Here it's 3, this one is 5, and this one is 9.
So I'm actually just going to create a temporary array here,
just because it's easier to read.
It's just an array by index.
So the maximum for the first one is "2."
For the second one, it's-- what did I say?--
"3."
The third character actually has nothing.
So let's just leave that empty.
Then we get "5."
And then we get "9."
Sorry for all the quotation marks.
Now we can use this here to set s i is the--
whatever the max value is for that i that we're on.
Let's see if I got everything.
I think this one might work.
I probably need to run it.
There's probably some syntax errors for sure in there.
But let's quickly see what happens
if we walk through this s and these targets.
So splitting it into characters, ignoring that, initial d is 0.
We're going to start at i 0.
So that's the 2's.
Yep.
So s i is not a question mark.
So we're going to skip this.
s i-- sorry, s i is not a question mark.
So we're going to go in here.
Oops, my bad.
t, that's the same.
That's the same.
Nothing happens here.
It's not a question mark.
So we're doing nothing, which is what we want.
Then we get to the second digit. i is 1.
The value is 3.
This one is a question mark here.
Correct.
So that means we can't do anything here.
So we go in here.
It's a question mark.
So then d is still 0.
So we're copying over the 3, which is correct.
We're going to roll out of this.
I'm going to go back here.
We're going to do another question mark,
copying off the values.
That's a 5.
So that one works.
Let's try the same here.
The target is 0.
So these are all 0's.
It's not a question mark. d is still 0.
They're the same.
So the d value doesn't change.
Then we get to the 8 and the 9.
So it's not a question mark. d is still 0.
t is greater than it is because we have a 9 and an 8.
So my d becomes plus 1.
And we never go in here again because now d has a value.
It's not a question mark.
So we're good.
We're skipping the 4 because that's also not a question mark.
That leaves us with this question mark.
So i is now 4.
We have a question mark.
d is 1, which means that we copy over the value for max,
which is 9.
So we end up with 8:49.
I think this works.
[CHEERING SOUND EFFECT]
So one possible solution is to brute-force
it to list all possible times for a given time
and then keeping track of the closest
time with reference time.
I think that's probably what I'd do, actually, is my guess.
[MUSIC PLAYING]
Actually, I don't think that's what-- hold on.
What do they mean there?
I really think my solution works here.
And what I like about it is that it handles every character
only once.
It's a bunch of ifs.
That's the part I don't like about it.
But we probably could optimize a bit there with, for example,
like I do here with the array.
That's one of the things to do it.
The only alternative I could think of
is essentially what I started with,
converting all the possible times
that you could have to minutes.
But that would mean that you need
to iterate all of the values here, which, in this case,
gives us only 10 options.
But if you look at here, we could
have many hundreds of options that we need to check.
That said, it's a much more simple numeric solution.
But I think mine works.
I'm actually quite happy with it.
So think I passed the interview.
Nice.
[MUSIC PLAYING]