Assignment #1
Assignment #1
2
BITS Pilani, Pilani Campus
Question2
Design a data structure SpecialStack that supports all the stack operations like
push(), pop(), isEmpty(), top() and an additional operation getMin() which
should return minimum element from the SpecialStack. All these operations of
SpecialStack must be O(1). To design SpecialStack, you should only use
standard Stack data structure and no other data structure like arrays, list, etc.
3
BITS Pilani, Pilani Campus
Question3
Describe a O(nlog(n))-time algorithm that, given a set S of n integers in sorted
order and another integer x, determines whether or not there exist two
elements in S whose sum is exactly x.
4
BITS Pilani, Pilani Campus
Question4
Given an unsorted array A of size n that may contain duplicates and a number k
< n, design an O(n) algorithm that returns true if array contains duplicates
within a distance of k, ie. Ǝ i, j Ɛ [0, n − 1] such that A[i] = A[j] and |i − j| < k.
5
BITS Pilani, Pilani Campus
Question5
You are given two binary search trees (not necessarily balanced). Design an
algorithm that merges the two given trees into a balanced binary search tree
in linear time.
6
BITS Pilani, Pilani Campus
Question6
Consider a stack with an additional operation, MULTIPOP(S, k) which removes
the k top objects of stack S, popping the entire stack if the stack contains
fewer than k objects. The cost of the operation MULTIPOP(S, k) is k, while
that of PUSH(S, x) and POP(S) is 1. Now consider a sequence of n stack
operations on an initially empty stack, where each operation is either PUSH,
POP or MULTIPOP. Prove that the total cost of these n operations is θ(n)
7
BITS Pilani, Pilani Campus
Question7
Provide an efficient algorithm for finding the kth Object of a Binary Tree
8
BITS Pilani, Pilani Campus
Question8
Provide an efficient algorithm for finding the successor & predecessor of the Binary
tree
9
BITS Pilani, Pilani Campus
Question9
One question of Hashing using quadratic Probing, list of elements & hash
functions are to be generated randomly.
10
BITS Pilani, Pilani Campus
Question10
Give an O(n)-time algorithm for Collapse problem (“in ONE loop”, & InPlace )
INPUT
A B C D E F G
OUTPUT
A B C D E F G
11
BITS Pilani, Pilani Campus
Question11
Give an O(n)-time algorithm for Collapse problem (“in ONE loop”, & InPlace )
INPUT
A B C D E F G
OUTPUT
A B C D E F G
12
BITS Pilani, Pilani Campus
Question12
Give an O(n)-time algorithm for computing the depths of all the nodes of a
tree T, where n is the number of nodes of T.