You are on page 1of 1

Hashing

1. A certain university assigns each of its students student numbers the first
time they register for any course. These numbers are sequential integers that
started at 0 many years ago and are now in the millions. Suppose we have a
class of one hundred first year students and we want to assign them hash
codes based on their student numbers. Does it make more sense to use the
first two digits or the last two digits of their student number? Justify your
answer.
2. Cuckoo Hashing for Undergraduates.
Binary Search Tree
1. Draw the BST that results when you insert items with keys
EASYQUESTION
in that order into an initially empty tree.
2. Suppose we have int values between 1 and 1000 in a BST and search for 363.
Which of the following cannot be the sequence of keys examined.
(a) 2 252 401 398 330 363
(b) 399 387 219 266 382 381 278 363
(c) 3 923 220 911 244 898 258 362 363
(d) 4 924 278 347 621 299 392 358 363
(e) 5 925 202 910 245 363
Object Oriented Programming
A queue can be implemented using two stacks. Let queue to be implemented be q
and stacks used to implement q be stack1 and stack2. q can be implemented in two
ways:
Method 1 (By making enQueue operation costly)
enQueue(q, x)
1) While stack1 is not empty, push everything from satck1 to stack2.
2) Push x to stack1 (assuming size of stacks is unlimited).
3) Push everything back to stack1.
dnQueue(q)
1) If stack1 is empty then error
2) Pop an item from stack1 and return it

Method 2 (By making deQueue operation costly)


enQueue(q, x)
1) Push x to stack1 (assuming size of stacks is unlimited).
deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything from satck1 to stack2.
3) Pop the element from stack2 and return it.

Which method is better to create queue using two stacks? Implement both methods
in Java. Hint: create Stack class in Java, then create two Stack instances/objects.

You might also like