Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 111 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
111
Dung lượng
15,46 MB
Nội dung
If you find the book helpful, please purchase a copy to support the authors!
11
Adnan
Aziz
is a professor at the
Department
of Electrical
and
Computer
Engineering
at
The University of Texas
at
Austin
,
where
he
conducts re-
search
and
teaches classes
in
applied algorithms.
He
received his
PhD
from The University of California at Berkeley; his
undergraduate
degree
is from IIT Kanpur.
He
has
worked
at Google,
Qua
1c
omm
, IBM,
and
sev-
eral software
startups.
叭Th
en
且
ot
designing algorithms,
he
plays
withhis
children, Laila,
Imran
,
and
Omar.
Amit
Prakash
is a Member of the Technical Staff
at
Google,
where
he
works
primarily
on
machine learning problems
that
arise
in
the context
of online advertising. Prior to
that
he
worked
at
Microsoft
in
the
web
search
te
缸
n.
He
received his
PhD
from TheUniversity ofTexas at Austin;
his
undergraduate
degree is from IIT
Kanpur.
叭Th
en
he
is
not
improving
the quality of
ads
,
he
indulges
in
his passions for
puzzles
, movies, travel,
and
adventures
with
his wife.
All rights reserved.
No
part
of this
publicatio
丑
may
be
reproduced,
stored
in
a retrieval system,
or
transmitted,
in
any
form
,
or
by
any
means
,
electronic, mechanical, photocopying, recording,
or
otherwise,
without
the
prior
consent of the authors.
This
book
was
typeset
by
the authors
using
Lesley
L
缸
npor
t'
s
匹趴
document
preparatio
丑
system
and
Peter Wilson's Memoir class.
The cover design
was
done
using
In
kscape. MacOSaiX
was
used
to
cre
伽
ate the front cover image; it approximates Shela
Nye's
portrait
of
Alan
Turing
using
a collection of public
domain
images of famous
computer
scientists
and
mathematicians.
古
le
graphic
on
the
back
cover
was
cre-
ated
by
Nidhi
Rohatgi.
The
companion
website for the
book
includes a list of
known
errors for
each version of
the
boo
k.
If
you
come across a technical error, please
write to
us
and
we
will cheerfully
send
you
$0.42. Please refer to the
website for details.
Ver
咀
on
1.
0.0 (September I, 2010)
L
叩
ebsite:http://algorithmsforinterviews.com
ISBN:
1453792996
EAN-13:
9781453792995
To
my
father!
Ishrat
Aziz!
for
giving
me
my
l~
作
long
love
of
learning
AdnanAziz
To
my
parents!
Manju
Shree
and
Arun
Prakash!
the
most
loving
parents
I
can
imagine
Amit Prakash
If you find the book helpful, please purchase a copy to support the authors!
Table
of
Contents
Prologue·1
Problem
Solving
丁
'echniques
. 5
I
Problems
13
1 Searching.
14
2 Sorting.
23
3
Meta
四
algorithms.
29
4 Algorithms
on
Graphs·
41
5 Algorithms
on
Strings·
52
6
Intractability.
56
7 Parallel Computing·
62
8 Design Problems·
67
9 Discrete Mathematics·
73
10
Probability·
80
11
Programming·
88
II
The
Interview
99
12
Strategies
For
A Great Interview·
100
13
Conducting An Interview·
105
If you find the book helpful, please purchase a copy to support the authors!
Let's begin
with
the picture
on
the
front cover.
You
may
have
observed
that
the
portra
让
of
Alan
Turing is constructed from a
number
of pictures
("tiles") of great
computer
scientists
and
mathematicians.
Suppose
you
were
asked
in
an
interview to design a
program
that
takes
an
垃
nage
and
a collection of s x s-sized tiles
and
produce
a mosaic
from the tiles
that
resembles
the
image. A good
way
to
begin
may
be
to
partition the image into s x s-sized squares,
compute
the
average color
of each
such
image square,
and
then
find the tile
that
is closest to it
in
the color space. Here distance
in
color space can
be
L2-norm
over
Red-
Green-Blue
(RGB)
intensities for
the
color. As
you
look
more
carefully at
the problem
,
you
might
conclude
that
it
would
be
better to
match
each
tile
with
an
image square
that
has
a similar structure.
One
way
could
be
to
perform
a coarse pixelization
(2
x 2
or
3 x
3)
of
each
挝
lage
square
and
finding the tile
that
is "closest" to
the
image square
under
a distance
V1
TABLE
OF
CONTENTS
III
Solutions
109
l
Searching· 110
2
Sorting'
123
3 Meta-algorithms· 130
4 Algorithms
on
Graphs· 144
5 Algorithms
on
Strings·
156
6 Intractability· 160
7 Parallel Computing· 167
Prologue
8 Design Problems· 174
9 Discrete Mathematics· 186
10 Probability· 194
11
Programming· 206
Index of Problems· 212
工
ι~N
时R
\T~
于C.Pt
N
.s
O
R "
2::Tit13;?32L;t23
山附吨吵
77
γ
au
ARf
/ /
SOMEW
且已民
E
//
10
0\
I
INYεNTE
j)
\坠主)
A NEWMo):>
E.
L
I)
/
o~
Cot
吨
Pυ
1'
A
,
ION
/
五夺
(!?Cn
MY
A.
t
喝
oS
一一 lit干气"
iau
c.
H
G.
VE
R.'f
U
/"
P
Pl
CK
E'T
oN
\~
THE
INT€
民
NeT
/
Figure 1. Evolution of a
computer
scientist
If you find the book helpful, please purchase a copy to support the authors!
2
PROLOGUE
3
function
defined
over
all pixel colors (for
example
, L2-norm
over
RGB
alues for
each
pixel).
Depending
0
日
how
you
represent
the
tiles,
you
eI1d
up
with
the
problem
of findirlg
the
closest
point
koma
set
of
pohts
in a
k-dimensional
space.
If
there
are m tiles aRd
the
image
is partitiORed
into
nsquaresr
then
a brute-force
approach
would
have
O(m·
η)
time
complexity. You
could
improve
0
口
this
by
first
indexhg
the
tiles
ushg
aIIappropriate
search
tree.Amore
detailed
disωsion
on
this
approach
is
presented
in
Prob-
lem
8.1
and
its solution.
If h a
E-60miRUte
hterviewy
you
can
work
thTough
the
above ideasr
write
some
pseudocode
for
your
algorithm
,
and
analyze its complex-
iiF
youwo
讪
d
have
had
a fairly successful
ir
阳忧
w.
In
丑叼
1p
归
a
盯
M
削
r
时叫
ticu
吐
1
址阳
lar
艾盯:
would
have
d
由
em
丑
10
∞
I
丑
1st
位
ra
挝
ted
tωo
your
in
口
1t
怡
erv
飞
vi
坦
ewe
盯
rt
也
ha
挝
t
you
possess several
key
skills:
_
The
ability
to
rigorously formulate
real-world
problems.
一Th
e
skills
to
solve
problems
and
design
algorithms.
一
The
tools
to
go from
an
algorithm to a
working
program.
一
The
malytical
techMques
required
to
determhe
the
computatioml
complexity of
your
solution.
Book
Overview
Alσorithms
for
In
terviews (AFI) aims
to
help
engineers
interviewing
for
SOLar-e
developnmtpmωns.
The
严
in
叫
T
foω
仙
FI
is
algorithm
design.
The
entire
book
is
prese
口
ted
through
problems
interspersed
with
discussions.
白
1e
problems
cover
key
comepts
md
are well-motivatedr
challenging,
and
fun
to
solve.
We
do
not
emphasize
platforms
and
programmi
哆
languages
since
they
differ across jobsy
md
cm
be
acquired
fairly
emly.II1terviews
at
st
large software
compmies
focus
more
on
algorithmsr
problem
solv-
iLaJdesign
skills
than
O
丑
specific
domain
knowledge.
Also,
pI
斗
fobs
aM
progmm1hgla
吨
mges
cm
chmge
quickly as requirements
chmge
but
the
qualities mmtiORed above
will
always
be
hmdameI1tal to
anv
successful software endeavor.
JTM
questiom
we
pment
should
allbe
solvable
withh
a
om
hour
iew
and
in
rna
叮
cases
,
take
s
由阳巾
lly
less time. A
question
may
take
more
or
less
time
to completeF
depmdhg
OIIthe
amOUIIt
of
oding
that
is
asked
for.
。品
soldomvaryhtems
ofdetail
m-for
some
pdlemswe
prese
口
t
detailed
implementations
in
Java/C
十十
IPytho
刊
for
othersr
we
siTPly
sketch solutions. Some
use
fairly technical
machinery
, e.g., max-
t1
ow
,
raI1domized malysisy
etc.You
will
enComter
such
problems
only
if
you
claim specialized
knowledge
, e.g.,
graph
algorithms, complexity
theory
,
etc.
In
terviewing
is
about
more
than
being
able to
design
algorithms
quickly. You also
need
to
know
how
to
present
yourself,
how
to
ask
for
help
when
you
are
stuck
,
how
to
come across as
being
excited
about
the
company
,
and
knowing
what
you
can
do
for them. We discuss
the
non-
technical aspects of
interviewing
in
Chapter
12.
You
can
practice
with
friends
or
by
yourself;
in
either case,
be
sure
to
time
yourself.
In
terview
at
as
many
places as
you
can
without
it
taking
away
from
your
job
or
classes. The experience
will
help
you
and
you
may
discover
you
like
companies
that
you
did
not
know
much
abou
t.
Although
an
interviewer
may
occasionally
ask
a
question
directly
from
AFI,
you
should
not
base
your
preparation
on
memor
恒
ing
solu
用
tions from AFI. We sincerely
hope
that
reading
this
book
will
be
enjoy-
able
and
improve
your
algorithm
design
skills. The
end
goal is
to
make
you
a
better
engineer
as
well
as
better
prepared
for software interviews.
Level
and
Prerequisites
Most
of AFI requires its
readers
to
have
basic familiarity
with
algorithms
taught
in
a typical
undergr
叫
uate-Ievel
algorithms
class.
古
le
chapters
O
口
meta-algorithms
,
gr
叩
hs
,
and
intractability
use
more
advanced
ma-
chinery
and
may
require
additional
review.
Each
chapter
begins
with
a
review
of
key
concepts. This
review
is
not
meant
to
be
comprehensive
and
if
you
are
not
familiar
with
the
material
,
you
should
first
study
the
corresponding
chapter
in
an
algorithms
text-
boo
k.
There are
dozens
of
such
texts
and
our
preference is
to
master
one
or
two
good
books
rather
than
super
血
cially
sample
many.
We
like Algo-
rithms
by
Dasgupta
,
Papadirnitriou
,
and
Vazirani
because
it
is succinct
and
beautifully written;
Introduction
to
Algorithms
by
Cormen
, Leiserson,
Ri
vest
,
and
Stein is
more
detailed
and
serves as a
good
reference.
Since
our
focus is
on
problems
that
can
be
solved
in
an
interview
rel-
atively completely, there are
many
elegant
algorithm
design
problems
which
we
do
not
include. Similarly,
we
do
not
have
any
straightforward
review-type problems;
you
may
want
to
brush
up
∞
these
using
intro-
ductory
programming
and
data-structures
texts.
The field
of
algorithms is
vast
and
there
are
many
specialized topics,
such
as
computational
geometry
,
numerical
analysis, logic
algori
仕
lms
,
etc. Unless
you
claim
knowledge
of
such
topics,
it
is
highly
unlikely
that
you
will
be
asked
a
question
which
requires esoteric
knowledge.
While
an
interview
problem
may
seem
specialized at first glance,
it
is
invariably
the case
that
the
basic
algorithms
described
in
this
book
are sufficient
to
solve i
t.
If you find the book helpful, please purchase a copy to support the authors!
4
Acknowledgments
The problems
in
this
book
come from diverse
sources-our
own
expe-
riences
, colleagues, friends, papers, books,
In
ternet bulletin boards, etc.
To
paraphrase
Paul
Halmos from his
wo
口
derful
book
Problems
for
Math-
ematicians
,
Young
and
Old:
"I
do
not
give
credits-who
discovered what?
Who
was
first? Whose solution is the best?
It
would
not
be
fair to give
credit
in
some
cases
缸
1d
not
in
others.
No
one
knows
who
discovered the
theorem
that
bears Pythagoras'
name
and
it does
not
matter. The
beauty
of the subject speaks for itself
and
so be it."
One
person
whose
help
and
support
has
improved
the quality of this
book
and
made
it fun to
read
is
our
cartoonist, editor,
and
proofreader,
Nidhi
Rohatgi. Several of
our
friends
and
students
gave feedback
on
this
book-we
would
especially like to
thank
Ian
Varley,
who
wrote so-
lutions to several problems
,
and
Senthil Chellappan, Gayatri Ramachan-
dran
,
and
Alper Sen for proofreading several chapters.
We
both
want
to
thank
all the people
who
have
been
a source of en-
lightenment
and
inspiratio
口
to
us
through
the years.
1/
Adnan
Aziz,
would
like to
thank
teachers, friends,
and
students
from IIT
Kanpur
,
UC
Berkeley,
and
UT Austin. I
would
especially like
to
thank
my
friends Vineet
Gupta
and
Vigyan Singhal,
and
my
teach-
ers Robert Solovay
, Robert Brayton, Richard Karp/
Raimund
Seidel,
and
Somenath Biswas for introducing
me
to the joys of algorithms. My co-
author
,
Amit
Prakash
, has
been
a
wonderful
collaborator-this
book
is a
testament to his intellect
, creativity,
and
enthusiasm.
1/
Amit
Prakash, have
my
co-author
and
mentor
,
Adnan
Aziz, to
thank
the
most
for this
boo
k.
To
a great extent,
my
problem
solving skills
have
been
shaped
by
Adnan. There have
been
occasions
in
life
when
I
would
口
ot
have
made
吐
Hough
without
his help.
He
is also the best
possible collaborator I can think of for any intellectual endeavor.
Over the years
, I have
been
fortunate to
have
great teachers at
IIT
Kanpur
and
UT Austin. I
would
especially like to
thank
Professors Scott
Nettles, Vijaya Ramachandran,
and
Gustavo
de
Veciana. I
would
also
like to
thank
my
friends
and
colleagues
at
Google, Microsoft,
and
UT
Austin for all the stimulating conversations
and
problem solving ses-
sions. Lastly
and
most
importantl
)T,
I
want
to
thank
my
family
who
have
been
a constant source of
support
,
exciteme
时/
and
joy for all
my
life
and
especially
during
the process of writing this boo
k.
ADNAN
AZIZ
ad
丑臼
l@a工
gorithmsforinterviews.com
AMIT
PRAKASH
amit@algorithmsforinterviews.com
Problem
Solving
Techniques
It
's
not that
I/m
so
smartt it's just
that I stay with problems
longer.
A.
Einstein.
-Developing
problem solving skills
is1ikekamizlg
to p1ay a m ·
instrument
一】-a
book
or
a teacher
cm
poht
you
h
the
right directiOIL
but
O
干
ly
your
hayd
workwill
take
you
where
you
want
to
g0·Like
a
m-
/
youn
创
tohowunde
蝴吨
concepts
but
theory is
no
substitute
for practice;for
th1s
reasonr AFI consists primarily ofproblems
Great problem
sokers
have
ski11s
that
carmot
be
captured
by
a set of
rules.Stilly
whm
faced
with
a cEIdleI1ging algoyithm desigIIprob1em
it
is
rlpfu1Mwe
a
sma11setdgm
向阳
iples
that
may
be
applicable
we
eIImerate
a c01lection of
such
prhciples
h Table
1.ofteIL
you
may
have
to
use
more
than
one of these
techdques.
We
will
I1ow
look
at
some
concrete examples of
how
these techRiques
an
be
applied.
DIVIDE-AND-CONQUER
AND
GENERALIZATION
A
triomho
is formed
by
joining three unit-sized squares
in
m L-shape.
Amu
也
ted
ches
由
oar
斗
(he
丑
cefor
也
8
x 8 Mboard) is
made
up
of 64 unit-
sized squares
arzmged
m m 8 × 8squarey
miI111s
the
topleft
squaye-sup-
2:12oua
盯
r
把.它
eas
咏
ωk
om
口
lin
丑
10ωst
白
ha
挝
t
covers the 8 x 8
Mb
o
侃
ard.
(Since there are
63
squares
h the 8 ×
8Mboard
and-we
have
ntriomhosr
a
valid
phcement
canmt
have overlapping triommos
or
trioIIlinos
which
extend
out
of
the
8 × 8
Mboard.)
Divide-aI1d-COIlquer is a
good
strategyto attackthis problem-k1stead
of
the 8 × 8Lfboardr1etFs consider m n ×
nLfboard-A2
×
2Mboard
c-n
be
covered
w
圳
triomir
叫阳比
of
tl
盯
ame
exact shape.
You
阳
m41
二
Z
;
i
己
2?
吃
:3:2:::;
江
i
工::?俨俨
hat
挝
ta
创阳
om
红
min
丑
linO
口∞
1
旧盯
O
叩
pI
阳丑
m
阳
nen
臼
m
叫
n
S
臼
sing
can
be
used
to
c
∞
omput
怡
e
a
plac
臼
em
工丑
len
丑
lt
for
an
r
ηZ
叶十
Ixη
十
1
If you find the book helpful, please purchase a copy to support the authors!
6
PROBLEM SOLVING TECHNIQUES
7
Technique Description
Divide-and- Can
you
divide the
problem
into
two
or more
conquer smaller
independent
sUbproblems
and
solve
the original problem
using
solutions to the
subproblems?
Recursion
, dynamic
If
you
have access to solutions for smaller in-
programmmg
stances of a givenproblem,
can
you
easily con-
struct a solution to the problem?
Case analysis
Can
you
split the
input/
execution into a
num-
ber
of cases
and
solve each case
in
isolation?
Generalization
Is there a problem
that
subsumes
your
prob-
lem
and
is easier to solve?
Data-structures
Is there a data-structure
that
directly
maps
to
the given problem?
It
erative refinement
Most problems can
be
solved
using
a brute-
force approach.
Can
you
formalize
such
a so-
lution
and
improve
up
∞
it?
Small examples
Can
you
find a solution to small concrete in-
stances of the problem
and
then
build
a so-
lution that can
be
generalized to
arbitrary
让卜
stances?
Reduction
Can
you
use
a problem
with
a
known
solution
as a subroutine?
Graph
modeling
Can
you
describe
your
problem
using
a
graph
and
solve it
using
an
existing algorithm?
Write
an
equatio
丑
Can
you
express
relatio
口
ships
in
your
problem
in
the form of equations (or inequalities)?
Auxiliary elements
Can
you
add
some
new
element to
your
prob-
lem
to get closer to a solution?
Variation
Can
you
solve a slightly differentproblem
and
map
its solution to
your
problem?
Parallelism
Can
you
decompose
your
problem
into sub-
problems
that
can
be
solved independently
on
different machines?
Caching
Can
you
store some of
your
computation
and
look it
up
later to save work?
Symmetry
Is there symmetry
in
the
input
space or solu-
tion space that
car
飞
be
explo
让
ed?
Table 1.
Commo
日
problem
solving techniques.
Mboard-However
you
wmquickly
see
tht
tkislhe
of
reasonhg
does
not
lead
you
anywhere.
Another hypothesis is
that
if a placement exists for
an
n
xη
Mb
oard
,
then one also exists for a 2n x
2η
Mb
oard.
This does
wor
k:
take 4 n x n
Mboards
and
arrange
them
以
orma
严
x2ηsquareimuchawdthi
three of
theMboards
have
theEIIUSSIng square set
towards
the
center
a
时
one
Mb
oard
has
its
missi
吨叩
are
outward
to
coin
创
e
with
the
missing cornerof a
2ηx2η
孔1b
oard.
The gap
in
the
ce
口
ter
can
be
covered
with
a
trio
吨。
and
,
by
hypothesis,
we
~a~
cover the
4η
×
η
Mb
oards
with
triominos as well.
Her
丑
nee
aplacαemen
时
1
吐
te
以
χi
妇
st
怡
sforany
ηt
也
ha
挝
ti
扫
s
a
power
O
们
f
归
2.
In
丑叩
pa
缸削
I
挝
ωO
∞
I
丑川
1
used
inη
川
t
白
he
pm f cm be dimetly coded to
fh
d
th
e ac
tu
aiC
OL
e-::L
as well.
Obs
盯
e
阳恤
P
伊阳
ro
伪
blemd
由
err
红
mo
ella
臼
sg
伊
ene
咀
eraliz
泣
za
挝
tion
(from 8 x 8
tω02
俨饥
x2
俨
η).
RECURSION
AND
DYNAMIC
PROGRAMMING
Suppose
you
were
to desig1aI1algorithm
that
takes az111npareIIthesized
e
创
S
臼蚓
S
剖
i
∞町
C
∞
O
叫
1
干卢
1
让
t
牛归
gμad
创
d
出创
i
让挝
t
赳
i
妇阳
Oαna
叫
mu
均
lica
甜
ti
∞
O
句
perato
创创
r
吼.
thepa
盯
ren
丑
lt
白
he
臼
Sl
曰
za
拍
tio
∞
I
丑
1
t
出:h
a
挝
tm
工丑
laxi
垃
m
丑
li
坦
ze
臼
s
the
飞
value
淀
e
of the
expressio
∞
n.
For
mpley
the
expression5-3·4+6yields
my
of the followiIIgva111es:
-25
=
5
一
(3
,
(4
+ 6))
-13
=
5
一
((3
.
4)
+
6)
20 = (5 - 3) . (4 + 6)
-1
=
(5
一
(3
.
4))
十
6
14
= ((5 -
3)
.
4)
+ 6
S
」;芷:
2C
甲吟叫
S
由如
M
均中抄
C
∞
om
叫
pu
盹
t
怡
e
由
the
p
归
a
盯盯削
r
跄
ren
时.它吱
m
创叫
e
缸
m
创
I
丑时毗
1
吐巾
t
也
thesizatio
∞
I
口山
1
aX1
牛中叫
I
虹
m
削
n
旧
1
让
ize
臼
sits
怡
sva
叫
alue
叩
1
凡
e
今,
i
扰
t
is easy to ider
哟
T
the
optimum
top level
parenthesization
• pareRtheSIze
on
each side of the operators azld
determ
mt
whi
中
operator
r
虹叫
I
e
仅
cur
岛
Sl
凹飞
ve
c
∞
ompu
时
ta
挝甜
t
拄
io
∞
n
of the
rnaχi
垃
m
丑
III
曰
zln
口
19
pa
盯
ren
口时
t
白
he
臼
si
垃
za
剖
tim
丑
1
for
u
由
be
今产
pre
蚓
O
∞丑
m
S leads to repeated calls
with
idmtical
argume
时
s.
Dy-
programming
avoids these repeated computations;refer to Prob-
lem 3.11for a detailed
exposition-I
CASE
ANALYSIS
Y
沟
ou
are
gi
扣
Vmemaset
S
ofE
distincthtegm
mdaCPUthathas
aspecial
mstruetiOIL SORt-Ethat
cm
sort5htegers
h
OIIe
cycle.Your
task is
to
identi
命
the
3largest
integers h S
ushg
SORt-5to compaye
and
sort
subsets of
afurthermorer
you
must
miIIimize the
number
of calls to
SORT5.
If you find the book helpful, please purchase a copy to support the authors!
8
PROBLEM
SOLVING
TECHNIQUES
9
If
all
we
had
to compute was the largest integer
in
the set, the opti-
mum
approach
would
be
to form 5
di
司
oint
subsets 8
1
,…,
8
5
of 8/ sort
each subset
,
and
then
sort {max8
1
,…,max 8
5
}.
This takes 6 calls to
SORT5
but
leaves ambiguity about the second
and
third
largest integers.
It
may
seem like
many
calls to
SORT5
are still needed. However if
you
do
a careful case analysis
and
eliminate all
x
ε8 for
which
there are
at least 3 integers
in
8 larger
than
X/
0
口
ly
5 integers remain
and
hence
just one more call to
SORT5
is
needed
to
compute
the result. Details are
given
in
the solutionto Problem 2
.5.
FIND
A
GOOD
DATA STRUCTURE
Suppose
you
are given a set of files, each containing stock quote infor-
mation. Each line contains starts
with
a
timest
缸丑
p.
The files are
indi
飞
rid
ually sorted
by
this value. You are to design
an
algorithm that combines
these quotes into a single file R containing these quotes
, sorted
by
the
出
nestamps.
This problem can be solved
by
a multistage merge process,
but
there
is a trivial solution using a min-heap
data
structure,
where
quotes are
ordered
by
timestamp. First
build
the min-heap
with
the first quote from
eachfile;
then
iteratively extract the
minimum
entry
e from the min-heap/
write
让
to
R,
and
add
in the next entry in the file corresponding to
e.
Details are given
in
Problem
2.10.
ITERATIVE REFINEMENT OF BRUTE-FORCE
SOLUTION
Consider the problem of string search (cf Problem 5.1): given
two
strings
s (search string)
and
T (text), find all occurrences of s
in
T. Since
scan
occur at
any
offset
in
T, the brute-force solution is to test for a
match
at
every offse
t.
This algorithm is perfectly correct; its time complexity is
O(η.
m)/
where
n
and
m are the lengths of
sand
T.
After trying some examples
,
you
may
see
that
there are several
ways
in
which
to
ir
口
prove
the time complexity of the brute-force algorithm.
For example
, if the character T[i] is
not
present
in
s
you
can suitably ad-
vance the matching. Furthermore
, this skipping
works
betterif
we
match
the search string from its
end
and
work
backwards. These refinements
willmake the algorithm
very
fast (linear-time)
on
random
text
and
search
strings;
however
, the
worst
case
complex
让
y
remains
O(η
·m).
You can
make
the additional
obser
飞
ration
that
a partial match of s
which
does
not
result
in
a full match implies other offsets
which
cannot
lead
to full matches. For
ex
缸
nple
,
if
s
二
α
bd
α
beabe
and
iff
starting back-
wards
,
we
have
a partial match
up
toα
be
α
be
that
does
口
ot
result in a full
match
,
we
know
that
the next possible matching offset
has
to
be
at least
3 positions
ahead
(where
we
can
match
the
secondα
be
from the partial
match).
By
puttiI1g together these refinemeI1ts
you
will
have
arrived at
the
famous Boyer-Moore string search
algorithm-its
worstmcase time C
m-
plexityis
oh+m)(whichis
thebestpossible
ffomatheoreticalperspecm
tivek
it
is also one of
the
fastest
strhg
search
dFrithms
h practice.
SMALL
EXAMPLES
Problems that seem difficult to solve in the abstract, can become much
mo
时
ractable
when
you
examine small concrete instances. For
instan
二
co
且
sider
tl
时
ollowi
口
g
problem: there
a
时
00
clo
时
doors
alo
吨
a
…
ri
dorr
numbered
from1t0500.A
persOI1walks
through
the corridor
and
opens each door.AIIother
person
walks
through
tke corridor
and
closes
every alternate door.
Continuing
如
this
m
缸
me
乙
the
i-th
person
c
aI1d
toggles the position of every t-th
door
starthgfrom
door
t.y;
to determine exactly
how
many
doors are
opmafter
the 500-th persOII
has walked
through
the corridor.
It
is
very
difficult to solve this
problemushg
abstract
variables.How-
ever if
you
try
the
problem
for
ljp374710?md20doorsr
it
takes
mder
ammte
to see
that
the
doors
that
remah
opmare
l?479716.··F regard-
less of
the totalI1umber of
doors.The
pattern
is
obvious-the
doors
that
re
中
am
op
中
are
中
ose
numbered
by
perfect squares. Once
you
make
ths
cOImeetlOIL1tls easy tOEZ?ve
it
for
the
generalcase-HeIIce the
totd
number
of
open
doors is l
y500
J=
22.
Refer~to
Problem 9A for a detailed
solution.
REDUCTION
Conside
气
the
probkm
of
fiMing
if
om
st
血
g
is a rotation of the other,
e.g.,
"car"
缸
1d
Harc"are rotatiORs
of
each other A I1aturd
approach
may
be
to rotate the first
strhgby
everypomible
offset
aM
ttmcomar4
wi
中
the
second
st
出
g.
This algorithm
would
have quadratic
ti
斗
com
plexity.
You
may
I1otice
that
this problem is quite
s
扛
nilar
to string search
which
cm
be
domh1inear-tmer
albeit
mhg
a
somewhat
complex
alm
gorithm.So
it
would
be
I1aturd to
try
to reduce this problem to string
search.IndeedrifwecomatemtethesecondstringwithitselfaMsearcE
for the first stying h tke resulting string,
we
will find a
match
iff the
two
original
strhgs
are rotatiOI1s of
each
other.This reduction yields a linear-
time algorithm for
our
problem;details
are giveR iRProbkII15.4.
Usually
you
try to reduce
your
proble~
to an easier problem. But
sometmesr
you
need
to reduce a problem bmWI1to
be
difficult to
your
giveI1problem to
show
that
your
problem
is difficult.Such
probkms
are
described in Chapter
6.
If you find the book helpful, please purchase a copy to support the authors!
10
PROBLEM
SOLVING
TECHNIQUES
11
GRAPH
MODELING
Drawing
pictures is a great
way
to
brainstorm
for a
potential
solution.
日
the
relati(;nships
in
a given
problem
can
be
represented
using
a
graph
,
auite often
the
problem
can
be
reduced
to a
well-known
graph
problem.
至
or
example,
suppose
you
are given a set of
barter
rates
between
com-
modities
and
you
are
supposed
to find
out
if
缸
1
arbitrage exists, i.
e.
, there
is a
way
by
which
you
can start
with
αunits
of
some
commodity
C
and
perform
a series of
barters
which
results
in
having
more
thanαunits
of
C.
We
can
model
the
problem
with
a
graph
where
commodities corre-
spond
to
vertices,
barters
correspond
t?
edges
,
.~~
the
:d?e
,:
eight
~s
s~t
to
the
logarithm
of
the
barter
rate.
If
we
can
find a cycle
in
the
graph
with
a
positke
weightrwe
wouldhave
fOUI1d
such
a series of
exchnges.
Such a cycle
can
be
solved
using
the
Bellma
扣
Ford
algorithm
(c
f.
Prob-
lem
4.19).
After some (or a lot) of
tr
划
-and-error
,
you
may
begin
to
wonder
if
a
such
a configuration exists.
Prov
hard.
However
if
you
think
of
the
8 x 8 square
board
as a chessboard,
you
will observe
that
the
removed
comers
are of
the
same
color. Hence
the
board
consists of either 30
white
squares
and
32 black squares
or
vice
versa. Since a
domino
will always cover
two
adjacent squares,
anyar-
rangement
of dominoes
must
cover the same
number
ofblack
and
white
squares. Hence
no
such
configuration exists.
The
or
地
inal
problem
did
not
talk
about
the
colors of
the
squares.
Adding
these colors to
the
squares makes
it
easy to
prove
impossibility,
illustrating the strategy of
adding
auxiliary elements.
VARIATION
WRITEANEQUATION
Some
problems
can
be
solved
by
expressing
them
in
the
language
of
mathematics. For example
,
suppose
you
were
aske~
to
write
an
algo-
rithm
that
computed
binomial
coefficien
怡,
G)
=硕兰布
The
problem
with
computing
the
binomial
coefficient directly from
the
definition is
that
the factorial function grows
very
quickly
and
can
overflow
an
integer variable.
If
we
use
floating
point
represe
口
tations
for
numbers
,
we
lose precision
and
the
problem
of overflow does
n?~
go
away. These
proble~s
potentially exist
even
if
the
final
value
.of
G)
i~
small.
One
c~
try
to factor the
numerator
and
denominator
and
try
and
cancel
out
commo
日
terms
but
factorization is itself a
hard
problem.
The
binomial
coefficients satisfy the
addition
formula:
Suppose
we
were
asked
to
design
an
algorithm
which
takes as
input
an
undirected
graph
and
produces
as
output
a black or
white
coloring of
the
vertices
such
that
for
every
vertex,
at
least half of its neighbors differ
in
color
from
让.
We
could
try
to solve this
problem
by
assigning arbitrary colors to
vertices
and
then
flipping colors
wherever
constraints are
not
me
t.
How-
ever this
approach
does
not
converge
0
口
all
examples.
It
turns
out
we
can
define a slightly different
problem
whose
solution
will yield
the
coloring
we
are looking for. Define
an
edge
to
be
diverse
if
its
ends
have
different colors.
It
is easy to verify
that
a color
assignment
that
maximizes
the
number
of diverse edges also satisfies
the
constraint
of
the
original problem. The
number
of diverse edges
can
be
maχ
迦
lized
greedily flipping
the
colors of vertices
that
would
lead
to a
higher
num-
ber
of diverse edges; details are
give
口
in
Problem 4.11.
PARALLELISM
//IlI\
+
-k
nk
In
the
context of interview
questio
口
s
,
parallelism is useful
when
dealing
with
scale, i.e.,
when
the
problem
is so large
that
it
is
红卫
possible
to solve
it
on
a single machine
or
it
would
take a
very
long
time. The
key
insight
you
need
to display is
how
to
decompose
the
problem
such
that
(1.)
each
subproblem
can
be
solved relatively
independently
and
(2.) constructing
the
solution to
the
or
培
inal
problem
from solutions to
the
subproblems is
not
expensive
in
terms
of
CPU
time
,
main
memory
,
and
network
usage.
Consider
the
problem
of
sorting
a petascale integer array.
If
we
know
the
distribution of
the
numbers
,
the
best
approach
would
be
to define
equal-sized ranges of integers
and
send
one
range
to
one
machine for
sorting. The sorted
numbers
would
just
need
to
be
concatenated
in
the
correct order.
If
the
distribution
is
卫
ot
known
then
we
can
send
equal-
sized arbitrarysubsets to
each
machine
and
then
merge
the
sorted
results
This
identity
leads to a straightforward recursion for
computing
(~)
which
avoids
the
problems
mentioned
above.
Dynamic
programming
has
to
be
used
to achieve
good
time
complexity-details
are
in
Prob
阳
lem
9.1.
AUXILIARY
ELEMENTS
Consider
an
8x 8
square
board
in
which
two
squares
0
且
diagonally
oppo-
site corners are removed. You are given a set of thirty-one 2 x 1 dominoes
and
are
asked
to cover
the
board
with
them.
If you find the book helpful, please purchase a copy to support the authors!
12
using
a min-heap. For details
on
petascale sorting, please refer to Prob-
lem2.2.
CACHING
Caching is a great tool
whenever
there is a possibility of repeating com-
putations. For example
, the central
idea
behind
dynamic
programming
is caching results from intermediate computations. Cachingbecomes ex-
tremely useful
in
another setting
where
requests come to a service
in
an
online fashion
and
a small
number
of requests take
up
a significant
amount
of
compute
power. Workloads
on
web
services exhibit this
prop-
erty; Problem
7.1
describes one such problem.
SYM
如
1ETRY
While
symmetry
is a simple concept it can
be
used
to
solve
very
difficult
problems
, sometimes
in
less
than
intuitive ways. Consider a 2-player
g
缸
ne
in
which
players alternately take bites from a chocolate bar. The
chocolate
bar
is
an
ηx
m rectangle; a bite
must
remove a square
and
all
squares above
and
to the right
in
the chocolate bar. The first
pI
句
rer
to eat
the
lower
leftmost square loses (think of
it
as
being
poisoned).
Suppose
we
are asked
whether
we
would
prefer to
play
first
or
sec-
ond.
One
approach
is to
make
the
obser
飞
ration
that
the
game is sym-
metrical for Player 1
and
Player
2/
except for their starting state.
If
we
assume
that
there is
no
winning
strategy for Player
1/
then
there
must
be
a
way
for Player 2 to
win
if Player 1 bites
the
top
right
square
in
his first
move.
Whatever
move
Player 2 makes after
that
can always
be
made
by
Player 1 as his
f
让
st
move. Hence Player 1
can
always win. For a detailed
discussion
, refer to
the
Problem
9.13.
CONCLUSION
In
addition
to
developing intuition for
which
technique
may
apply to
which
problem
, it is also
important
to
know
when
your
technique is
not
wor
烛
19
and
quickly move to
your
next
best
guess.
In
an
interview set-
ting/
even
if
you
do
not
end
up
solving the
problem
entirely,
you
will
get credit for
applying
these
tecm
问
ues
in
a systematic
way
and
clearly
communicating
your
approach to the problem.
We
cover nontechnical
aspects of
problem
solving
in
Chapter
12.
Part I
Problems
If you find the book helpful, please purchase a copy to support the authors!
[...]... right subchild 1 16 SEARCH BST FOR A KEY Searching for a key in a BST is very similar to searching in a sorted array Recursion is more natural but for performance, a while-loop is preferred Problem 1.16: Given a BST T, 丘rst write a recursive function that searches for key K , then write an iterative function 1.17 SEARCH BST FOR x> k BSTs offer more than the abili可 to search for a key-they can be used to... time lookups for the min element.) Searching for arbitrary keys has O(η) time complexity-a 町thi吨 that can be done with a heap can be done with a balanced BST with the same complexity but with possibly some space and time overhead 2.10 MERGING SORTED ARRAYS You are given 500 files , each containing stock quote information for an SP500 company Each line contains an update of the following form: 1232111... amount of chmge that remahs to h made If we try this for 48 pe口ce, we get 30 , 12 , 6 However the optimum would be 24 , 24 In its most general form, the coin changing problem is NPMrd(4.ChapteJ6)but for some coimgesr the greedy fl写frit』mm optimum-e-E-r if tt1e denomiRatiom are ofthe form {lAT27俨} Ad hoc guments can be applied to show that 让 is also optimum for VS coins iiZgeneralproblem ca由e solved in pseudopolynomial... algorithm is 如nd缸nental to the performance of many applications and there are several elega时 algorithms proposed for it, each with its own tradeoffs As a result, there is no one perfect answer to 让 If someone asks you this questio口 in an interview, the best way to approach this problem would be to work through one good algorithm in detail and discuss the breadth of other algorithms for solving this problem... tharIthe original proMem .For exampley COI1sider the following problem: give口 an array of integers A of length 凡 find the interval indices a and b such that 2:~=α A[i] is maximized Letrs try to solve this problem assumiRg we have the s0111tiORfor the subarray A[l , 饥- 1] In this case, even if we knew the largest sum subarray for array A[l , η-I], it does not help us solve the problem for A[l , η] Now, consider... Rehashing is expensive (e (η+ m) time) but if it is performed infrequently (for example, if performed every time the load increases by 2x) , its amortized cost is low Compared to binary search trees (discussed on Page 20), inserting and deleting in a hash table is more efficient (assuming the load is constant) One disadvantage of hashing is the need for a good hash function but this is rarely an issue... classmates in a graduate course on information theory at MIT were given the choice of a term paper or a final exam For the term pape乙 Huffman's professor, Robert M Fano, had given the problem of finding an algorithm for assigning binary codes to symbols such that a given set of symbols can be represented in the smallest number of bits Huf缸lan worked on the problem for months, developing a number of approaches... assign the shorter strings to more probable characters and the longer strings to less probable characters 3.12 SCHEDULING TUTORS ¥'ou are responsible for scheduling tutors for the day at a tutoring com- 卢町 For 二ach day, you have received a number of r叩臼ts for tutors Each wquest has a specified start time md each lessORis thirty miI111tes 10吨 YLu have more tutors ttm reqlmts.Each tutor cm start work at... 征伍 ime client 俑阳创ore t四 For example, if the lookups are s凹ed 削 1 i t ake st 耐.它吐urn 怡 臼 怡 吐 i口 order of 让lcreasing i , then the client making the i-th query has to wait 2:~=1 tj milliseconds Problem 3.13: Design an efficient algorithm for computing 缸loptimum order for processing the queries If you find the book helpful, please purchase a copy to support the authors! Greedy Algorithms HUFFMAN CODING... system that customers use to trigger certain tasks He wants to minimize the average amount of time it takes for a customer to perform tasks If a menu item is at the i-th positio凡 it takes i units of time for the user to reach there (linear scan) and it takes c units of time to click on it MEL ALGORITHMS Each menu item can have multiple levels of sub-menus and a sub-menu can be reached by clicking on . of
support
,
exciteme
时/
and
joy for all
my
life
and
especially
during
the process of writing this boo
k.
ADNAN
AZIZ
ad
丑臼
l@a工
gorithmsforinterviews.com
AMIT
PRAKASH
amit@algorithmsforinterviews.com
Problem
Solving
Techniques
It
's
not. cheerfully
send
you
$0.42. Please refer to the
website for details.
Ver
咀
on
1.
0.0 (September I, 2010)
L
叩
ebsite:http://algorithmsforinterviews.com
ISBN:
1453792996
EAN-13:
9781453792995
To
my
father!
Ishrat
Aziz!
for
giving
me
my
l~
作
long
love
of
learning
AdnanAziz
To
my
parents!
Manju
Shree
and
Arun
Prakash!
the
most
loving
parents
I
can
imagine
Amit