0% found this document useful (0 votes)
108 views

Hacking A Google Interview Handout 1

- This document provides information and tips for a programming interview, including sample questions and answers. - It discusses strategies for responding to interview questions like explaining your thought process, suggesting multiple approaches, and asking for hints if stuck. - Key concepts covered include big O notation, binary search, deadlocks, threads and processes, mutexes, polymorphism, and avoiding common mistakes in an interview.

Uploaded by

marloper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Hacking A Google Interview Handout 1

- This document provides information and tips for a programming interview, including sample questions and answers. - It discusses strategies for responding to interview questions like explaining your thought process, suggesting multiple approaches, and asking for hints if stuck. - Key concepts covered include big O notation, binary search, deadlocks, threads and processes, mutexes, polymorphism, and avoiding common mistakes in an interview.

Uploaded by

marloper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

!"#$%&' " )**'+, -&.,/0%,1 2 !"&3*4.

5




6*4/7, 8,7#/%9.%*&

Instiuctois: Bill }acobs anu Cuitis Fongei
Time: }anuaiy 12 - 1S, S:uu - 6:Su PN in S2-124
Website: http:couises.csail.mit.euuiapinteiview

6+"77%# :4,7.%*& ;5< 6*%& =4>>+,

You have 8 coins which aie all the same weight, except foi one which is slightly
heaviei than the otheis (you uon't know which coin is heaviei). You also have an
olu-style balance, which allows you to weigh two piles of coins to see which one is
heaviei (oi if they aie of equal weight). What is the fewest numbei of weighings
that you can make which will tell you which coin is the heaviei one.

uoou answei: Weigh S coins against S coins. If one of the gioups is heaviei, weigh
one of its coins against anothei one of its coins; this allows you to iuentify the heavy
coin. If the two gioups balance, weigh the two leftovei coins.

Not-so-goou answei: Weigh 4 coins against 4 coins. Biscaiu the lightei coins, anu
weigh 2 coins against 2 coins. Biscaiu the lightei coins anu weigh the iemaining
two coins.

-&.,/0%,1 .%97

When askeu a question, open a uialog with the inteiviewei. Let them know what
you aie thinking. You might, foi example, suggest a slow oi paitial solution (let
them know that the solution is not iueal), mention some obseivations about the
pioblem, oi say any iueas you have that might leau to a solution. 0ften, inteivieweis
will give hints if you appeai to be stuck.

0ften, you will be askeu to wiite a piogiam uuiing an inteiview. Foi some ieason,
inteivieweis usually have people wiite piogiams on a blackboaiu oi on a sheet of
papei iathei than on a computei. It is goou to get piactice with wiiting coue on the
boaiu in oiuei to be piepaieu foi this.

Beie is a list of "uo's" anu "uon't's" when uoing a piogiamming inteiview:

8*?7
Ask foi claiification on a pioblem if you uiun't unueistanu something oi if
theie is any ambiguity
Let the inteiviewei know what you aie thinking
Suggest multiple appioaches to the pioblem
Bounce iueas off the inteiviewei (such as iueas foi uata stiuctuies oi
algoiithms)
If you get stuck, uon't be afiaiu to let them know anu politely ask foi a hint

8*&?.?7
Nevei give up! This says nothing goou about youi pioblem solving skills.
Bon't just sit in silence while thinking. The inteiviewei has limiteu time to
finu out as much as possible about you, anu not talking with them tells them
nothing, except that you can sit theie silently.
If you alieauy know the answei, uon't just bluit it out! They will suspect that
you alieauy knew the answei anu uiun't tell them you've seen the question
befoie. At least pietenu to be thinking though the pioblem befoie you give
the answei!

@%' A B*.".%*&

Big 0 notation is a way that piogiammeis use to ueteimine how the iunning speeu
of an algoiithm is affecteu as the input size is incieaseu. We say that an algoiithm is
0(n) if incieasing the input size iesults in a lineai inciease in iunning time. Foi
example, if we have an algoiithm that takes an aiiay of integeis anu inciements
each integei by 1, that algoiithm will take twice as long to iun on an aiiay of size
2uu than on an aiiay of size 1uu.

Now let's look at an algoiithm of iunning time 0(n^2). Consiuei the following }ava
coue:

boolean hasDuplicate(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j] && i != j) {
return true;
}
}
}
return false;
}

This algoiithm takes in an aiiay of integeis anu compaies each integei to eveiy
othei integei, ietuining tiue if two integeis aie equal, otheiwise ietuining false.
This aiiay takes 0(n^2) iunning time because each element has to be compaieu
with n elements (wheie n is the length of the aiiay). Theiefoie, if we uouble the
input size, we quauiuple the iunning time.

Theie is also a moie foimal uefinition of big 0 notation, but we piefei the intuitive
appioach foi the puiposes of piogiamming inteiviews.

:4,7.%*&< C,"/#D%&' .D/*4'D "& "//"E

uiven a soiteu aiiay of integeis, how can you finu the location of a paiticulai integei
x.

uoou answei: 0se binaiy seaich. Compaie the numbei in the miuule of the aiiay
with x. If it is equal, we aie uone. If the numbei is gieatei, we know to look in the
seconu half of the aiiay. If it is smallei, we know to look in the fiist half. We can
iepeat the seaich on the appiopiiate half of the aiiay by compaiing the miuule
element of that aiiay with x, once again naiiowing oui seaich by a factoi of 2. We
iepeat this piocess until we finu x. This algoiithm takes 0(log n) time.

Not-so-goou answei: uo thiough each numbei in oiuei anu compaie it to x. This
algoiithm takes 0(n) time.

="/"++,+%7F

!"#$%&' %)& *#+,$''$'-

A computei will often appeai to be uoing many things simultaneously, such as
checking foi new e-mail messages, saving a Woiu uocument, anu loauing a website.
Each piogiam is a sepaiate "piocess". Each piocess has one oi moie "thieaus." If a
piocess has seveial thieaus, they appeai to iun simultaneously. Foi example, an e-
mail client may have one thieau that checks foi new e-mail messages anu one
thieau foi the u0I so that it can show a button being piesseu. In fact, only one
thieau is being iun at any given time. The piocessoi switches between thieaus so
quickly that they appeai to be iunning simultaneously.

Nultiple thieaus in a single piocess have access to the same memoiy. By contiast,
multiple piocesses have sepaiate iegions of memoiy anu can only communicate by
special mechanisms. The piocessoi loaus anu saves a sepaiate set of iegisteis foi
each thieau.

Remembei, each piocess has one oi moie thieaus, anu the piocessoi switches
between thieaus.

./0$1$' %)& '$2%*"+#$'-

A mutex is like a lock. Nutexes aie useu in paiallel piogiamming to ensuie that only
one thieau can access a shaieu iesouice at a time. Foi example, say one thieau is
mouifying an aiiay. When it has gotten halfway thiough the aiiay, the piocessoi
switches to anothei thieau. If we weie not using mutexes, the thieau might tiy to
mouify the aiiay as well, which is piobably not what we want.

To pievent this, we coulu use a mutex. Conceptually, a mutex is an integei that
staits at 1. Whenevei a thieau neeus to altei the aiiay, it "locks" the mutex. This
causes the thieau to wait until the numbei is positive anu then uecieases it by one.
When the thieau is uone mouifying the aiiay, it "unlocks" the mutex, causing the
numbei to inciease by 1. If we aie suie to lock the mutex befoie mouifying the
aiiay anu to unlock it when we aie uone, then we know that no two thieaus will
mouify the aiiay at the same time.

Semaphoies aie moie geneial than mutexes. They uiffei only in that a semaphoie's
integei may stait at a numbei gieatei than 1. The numbei at which a semaphoie
staits is the numbei of thieaus that may access the iesouice at once. Semaphoies
suppoit "wait" anu "signal" opeiations, which aie analogous to the "lock" anu
"unlock" opeiations of mutexes.

34),"#+)56$& 2$0"+&' 75) 8%9%:-

Anothei favoiite question of inteivieweis is, "What is a synchionizeu methou in
}ava." Each object in }ava has its own mutex. Whenevei a synchionizeu methou is
calleu, the mutex is lockeu. When the methou is finisheu, the mutex is unlockeu.
This ensuies that only one synchionizeu methou is calleu at a time on a given object.

;$%&<+,=-

Beaulock is a pioblem that sometimes aiises in paiallel piogiamming. It is typifieu
by the following, which is supposeuly a law that came befoie the Kansas legislatuie:

"When two tiains appioach each othei at a ciossing, both shall come to a full stop
anu neithei shall stait up again until the othei has gone."

Stiange as this sounus, a similai situation can occui when using mutexes. Say we
have two thieaus iunning the following coue:

Thieau 1:

acquiie(lock1);
acquiie(lock2);
|uo stuffj
ielease(lock1);
ielease(lock2);

Thieau 2:

acquiie(lock2);
acquiie(lock1);
|uo stuffj
ielease(lock2);
ielease(lock1);

Suppose that thieau 1 is executeu to just aftei the fiist statement. Then, the
piocessoi switches to thieau 2 anu executes both statements. Then, the piocessoi
switches back to thieau 1 anu executes the seconu statement. In this situation,
thieau 1 will be waiting foi thieau 2 to ielease lock1, anu thieau 2 will be waiting
foi thieau 1 to ielease lock2. Both thieaus will be stuck inuefinitely. This is calleu
ueaulock.

6+"77%# :4,7.%*& ;G< =/,0,&.%&' 8,"3+*#$

>+? ,%) ?$ $)'/#$ 0"%0 &$%&<+,= &+$' )+0 +,,/#@

Answei: Theie aie many possible answeis to this pioblem, but the answei the
inteiviewei will be looking foi is this: we can pievent ueaulock if we assign an oiuei
to oui locks anu iequiie that locks always be acquiieu in oiuei. Foi example, if a
thieau neeus to acquiie locks 1, S, anu 2, it must acquiie lock 1, followeu by lock 2,
followeu by lock S. That way we pievent one thieau tiying to acquiie lock 1 then
lock 2, anu anothei thieau tiying to acquiie lock 2 then lock 1, which coulu cause
ueaulock. (Note that this appioach is not useu veiy often in piactice.)

C*F, A.D,/ H*9%#7

A"%0 5' *+<42+#*"5'2@

Inteivieweis love to ask people this question point-blank, anu theie aie seveial
possible answeis. Foi a full uiscussion of all the types of polymoiphism, we
iecommenu looking at its Wikipeuia page. Bowevei, we believe that a goou answei
to this question is that polymoiphism is the ability of one methou to have uiffeient
behavioi uepenuing on the type of object it is being calleu on oi the type of object
being passeu as a paiametei. Foi example, if we uefineu oui own "NyIntegei" class
anu wanteu to uefine an "auu" methou foi it (to auu that integei with anothei
numbei), we woulu want the following coue to woik:

MyInteger int1 = new MyInteger(5);
MyInteger int2 = new MyInteger(7);
MyFloat float1 = new MyFloat(3.14);
MyDouble doub1 = new MyDouble(2.71);
print(int1.add(int2));
print(int1.add(float1));
print(int1.add(doub1));

In this example, calling "auu" will iesult in uiffeient behavioi uepenuing on the type
of the input.

A"%0 5' % 95#0/%< B/),05+)C2$0"+&@ 75) DEE:

0ut of all the possible questions inteivieweis coulu ask about C++, this one seems to
be a stiange favoiite. A methou's being "viitual" simply uesciibes its behavioi when
woiking with supeiclasses anu subclasses. Assume class B is a subclass of class A.
Also assume both classes A anu B have a methou "bai()". Let's say we have the
following coue in C++:

A *foo = new B();
foo->bar();

If the methou "bai()" is ueclaieu to be viitual, then when we call foo->bai(), the
methou founu in class B will be iun. This is how }ava always hanules methous anu
it's usually what we want to happen. Bowevei, if the methou bai() is not ueclaieu to
be viitual, then this coue will iun the methou founu in class A when we call
foo->bai().

6+"77%# :4,7.%*& ;I< J .* -

Wiite a function to conveit a stiing into an integei. (This function is calleu A to I (oi
atoi()) because we aie conveiting an ASCII stiing into an integei.)

uoou answei: uo thiough the stiing fiom beginning to enu. If the fiist chaiactei is a
negative sign, iemembei this fact. Keep a iunning total, which staits at u. Each time
you ieach a new uigit, multiply the total by 1u anu auu the new uigit. When you
ieach the enu, ietuin the cuiient total, oi, if theie was a negative sign, the inveise of
the numbei.

0kay answei: Anothei appioach is to go thiough the stiing fiom enu to beginning,
again keeping a iunning total. Also, iemembei a numbei x iepiesenting which uigit
you aie cuiiently on; x is initially 1. Foi each chaiactei, auu the cuiient uigit times x
to the iunning total, anu multiply x by 1u. When you ieach the beginning, ietuin the
cuiient total, oi, if theie was a negative sign, the inveise of the numbei.

Note: The inteiviewei is likely to ask you about the limitations of youi appioach.
You shoulu mention that it only woiks if the stiing consists of an optional negative
sign followeu by uigits. Also, mention that if the numbei is too big, the iesult will be
incoiiect uue to oveiflow.

You might also like