mockmidterm2 - CS
61B
 July
30,
2008
 
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS
61B
 July
30,
2008
 
 Mock
Exam

 Disclaimer:
This
is
mock
exam
is
designed
to
give
you
hints
for
your
review.
It
by
no
means
 resembles
the
actual
midterm
and
you
should
not
use
these
questions
as
the
only
basis
in
 your
exam
preparation.

 You
have
45
minutes
to
complete
this
mock
exam.
 A
 
 
 Problem
1
(Asymptotic
Analysis)
 
 Suppose
 f (x) ∈ O( g(x)) .
Assume
 and
 
are
positive
for
all
values
of
x.
For
each
of
the
 following
statements,
say
whether
it
must
be
true
or
must
be
false
or
could
be
either:
 
 €
 
 
 
 
 
 
 
 
 
 
 Problem
2
(Hashing)
 Suppose
we
have
defined
a
hash
set
for
storing
elements
of
class
MyClass
with
b
buckets
and
chains
 implemented
as
linked
lists.

First,
n
different
elements
are
stored
in
the
table.

Now
we
wish
to
store
 one
more,
called
newElem,
which
is
not
equal
to
any
of
the
others.

We'd
like
to
do
it
with
as
few
 comparisons
(calls
to
MyClass.equals)
between
newElem
and
other
elements
as
possible.

Ordinarily
 we'd
ensure
this
by
choosing
a
very
good
MyClass.hashCode
method.

But
there's
a
catch:
a
dastardly
 adversary
is
trying
to
make
the
number
of
comparisons
as
large
as
possible.
 
 In
terms
of
n
and
b,
what
is
the
fewest
possible
number
of
comparisons
it
will
take
to
add
newElem
to
 the
table
if:
 
 a)
The
adversary
gets
to
choose
MyClass.hashCode(),
but
then
we
get
to
choose
newElem
(knowing
his
 choice).

What
does
his
MyClass.hashCode
look
like?

You
don’t
need
to
give
any
actual
code.
 
 
 
 
 
 
 
 
 
 
 
 
 
 A2
 
 
 
 b)
The
adversary
gets
to
choose
newElem,
but
then
we
get
to
choose
MyClass.hashCode
(knowing
his
 choice).

What
does
our
MyClass.hashCode
look
like?
 
 
 
 
 
 
 
 
 
 
 c)
We
get
to
choose
MyClass.hashCode,
and
then
the
adversary
gets
to
choose
newElem
(knowing
our
 choice).
 
 
 
 
 
 (Optional
bonus
question—do
this
part
last)
 
 Now
suppose
we
implement
the
hash
table
in
a
different
way:
rather
than
storing
the
chains
as
linked
 lists,
we
store
them
as
binary
search
trees.
 
 d)
What
method
must
we
implement
in
MyClass
to
ensure
this
is
possible?
 
 
 
 
 Assume
through
some
wizardry
that
we
ensure
the
search
trees
are
maximally
balanced.

What
are
the
 answers
to
questions
a)
through
c)
above
with
this
new
data
structure,
with
comparisons
now
meaning
 a
call
to
the
method
given
as
the
answer
to
part
d)?
 
 a)
 
 
 
 
 b)
 
 
 c)
 
 A3
 
 
 Problem
3.
Binary
Tree

 a) Draw
the
binary
search
tree
created
by
inserting
these
values
in
this
order:
3
8
6
0
9
2
7
4
1
5
 
 
 
 
 
 
 
 
 
 
 b) Write
the
inorder
traversal
of
the
tree
you
just
drew.
 
 
 c) Given
the
following
class
definition:
 
 class BinaryTree{ BinaryTreeNode root; int size; class BinaryTreeNode{ int item; BinaryTreeNode parent; BinaryTreeNode left; BinarTreeNode right; } } 
 Write
a
method
largestKeyNotLargerThan(int k)
that
takes
an
integer
key
k
and
return
the
 largest
key
in
the
binary
tree
smaller
than
or
equal
to
k.
 

 
 
 
 
 A4
 
 
 A5
 
 
 Problem
4.
Sorting
 a) If
you
know
the
input
array
to
your
sorting
algorithm
is
almost
sorted,
which
sorting
algorithm
 would
you
use?

Why?
 
 
 
 
 b) Picking
a
good
pivot
is
essential
for
quicksort
to
run
efficiently;
a
bad
pivot
can
cause
the
algorithm
 to
degenerate
into
worst
case
 
running
time.
Assuming
that
a
particular
quicksort
 implementation
always
picks
the
element
in
the
middle
of
the
array,
come
up
with
an
adversarial
 input
(i.e.
worse‐case
input)
that
would
cause
the
corresponding
quicksort
algorithm
to
run
in
 time.
 
 
 A6
 
 ...
View Full Document

Ask a homework question - tutors are online