You are on page 1of 5

Solve the Questions from introduction to algorithm.

2.2-1, 2-1, 3.1-3, 3.1-7, 3-1, 3-2, 4.4-1, 4.4-7, 4.5-1, 4.5-4, 4-1

1. Show exactly why if we grouped elements into groups of 3 each, the median fin
ding algorithm that we discussed in class will not work in linear time. What wou
ld be the running time of the algorithm in this case?
2. Which is better in the median-finding algorithm, grouping into groups of 5, o
r into groups of 7? Explain your answer.
3. Describe the selection algorithm (k-th smallest number finding algorithm). An
alyze its time complexity.
4. Sort a linked list using insertion sort. What is the time complexity?
5. Merge two sorted linked lists. Analyze the time and space complexity.
6. Given a singly linked list, determine if it is a palindrome.
7. Reverse a singly linked list.
8. Write a program to find the node at which the intersection of two singly lin
ked lists begins.
9. Remove all elements from a linked list of integers that have value val.
10. Given a linked list, remove the nth node from the end of list and return its
head
11. Write an efficient algorithm to find the median of two sorted arrays each of
size n.
1. Write an efficient algorithm to find the maximum height of a tree.
2. Write an efficient algorithm to find the minimum height of a tree.
3. Given a complete binary tree, count the number of nodes. [ In a complete bin
ary tree every level, except possibly the last, is completely filled, and all no
des in the last level are as far left as possible.]
4. Given a sorted Linked list, create a balanced binary search tree out of it. [
Try to do it in O(n) time]
5. Given a binary tree, determine if it is balanced.
6. You are given the in-order and pre-order traverse results from a Binary tree.
Devise an efficient algorithm to recover the binary tree.
7. Instead of binary heaps, suppose you had to implement ternary heaps (i.e., wh
ere each node has up to three children). Explain how you would implement such he
aps using arrays, and how you can determine child and parent pointers. What are
the advantages/disadvantages of ternary heaps over binary heaps?
8. Can you use a binary search tree to simulate heap operations? What are the ad
vantages/disadvantages of doing so?
9. What is minimum possible number of nodes in a red-black tree which contains

two black nodes from every root-to-leaf path?


10. Suppose we define a red-black tree where along each path the number of blac
k nodes is the same, and there cannot be more than three consecutive red nodes (
but there may be two consecutive red nodes).
What is the ratio of the longest possible path length to the shortest possible p
ath length in this tree?
For a tree that has b black nodes along each path, what ratio of the maximum pos
sible number of nodes to the minimum possible number of nodes in the tree?
11. Write an efficient algorithm to count the number of nodes in a complete bina
ry tree. What is the time complexity?
12. Write an algorithm to print the right side view of a binary tree.
13. Write an algorithm using heaps to keep track of the median of a stream of nu
mbers. What is the time complexity?

1. Suppose you are given a graph that is almost disconnected, i.e., the removal of
a single edge will disconnect the graph. Design an algorithm to efficiently fin
d this edge.
2 . What are the kinds of graphs for which depth-first search orderings and brea
dth-first search orderings are the same?
3. Given a binary tree, find the maximum path sum. [path can be between any two
nodes in the tree]
4. Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X
'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
For example,
X X X
X O O
X X O
X O X
After

X
X
X
X
running your function, the board should be:

X
X
X
X

X
X
X
X

X
X
X
O

X
X
X
X

5. Recall the definition of the iterated logarithm function log*(n).


i. Similarly, try and define the iterated square root function sqrt*(n). Sqrt*(2^2
^2^2) = 4
ii. What is the value of sqrt*(2^32)? sqrt*(2^32)=5.
6. Write an algorithm that determines whether or not a given undirected graph G
= (V, E) contains a cycle. Your algorithm should run in O(V ) time, independent
of |E|. [Also explain clearly in your own words] [Hint: Use DFS]
7. Would you prefer DFS or BFS (or both equally) for the following tasks? (Assum

e the graph is undirected and connected.) Justify your answer.


a) Determining whether a graph is acylic
b) Finding a path to a vertex known to be near the starting vertex
c) Finding the connected components of the graph

8. Can you describe an algorithm to find the maximum spanning tree? What's the c
omplexity of the algorithm?
9. A weighted undirected graph with n vertices and m edges is said to satisfy th
e triangle inequality if for every edge (u, v), the weight of (u, v) is less tha
n or equal to the length of any other alternate path from u to v. a. Prove that
for such a graph, the total weight of all edges is <= (m-n+1)*MST, where MST is
the total weight of all edges of the minimum spanning tree.

Use concept learned from KMP:


1. Given a string S, you are allowed to convert it to a palindrome by adding cha
racters in front of it. Find and return the shortest palindrome you can find by
performing this transformation.
For example:
Given "aacecaaa", return "aaacecaaa".
Given "abcd", return "dcbabcd".
Use concept similar to Longest common subsequence:
2.Given a string S and a string T, count the number of distinct subsequences of
T in S.
A subsequence of a string is a new string which is formed from the original stri
ng by deleting some (can be none) of the characters without disturbing the relat
ive positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE
" while "AEC" is not).

Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
First try to use solve using exhastive/exponential/simple approach, then try to
apply dynamic programming:
3. There is a fence with n posts, each post can be painted with one of the k col
ors. You have to paint all the posts such that no more than two adjacent fence p
osts have the same color. Return the total number of ways you can paint the fenc
e. [Note: n and k are non-negative integers.]
4. There are a row of n houses, each house can be painted with one of the three
colors: red, blue or green. The cost of painting each house with a certain color
is different. You have to paint all the houses such that no two adjacent houses
have the same color.
The cost of painting each house with a certain color is represented by a n x 3 c
ost matrix. For example, costs[0][0] is the cost of painting house 0 with color
red; costs[1][2] is the cost of painting house 1 with color green, and so on...
Find the minimum cost to paint all houses.
5. There are a row of n houses, each house can be painted with one of the k colo
rs. The cost of painting each house with a certain color is different. You have
to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x k c
ost matrix. For example, costs[0][0] is the cost of painting house 0 with color
0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find t
he minimum cost to paint all houses.
Note:
All costs are positive integers.
Follow up:
Could you solve it in O(nk) runtime?
6. A robot is located at the top-left corner of a m x n grid.
The robot can only move either down or right at any point in time. The robot is
trying to reach the bottom-right corner of the grid (marked 'Finish' in the diag
ram below).
How many possible unique paths are there?
7. Say you have an array for which the ith element is the price of a given stock
on day i.
If you were only permitted to complete at most one transaction (ie, buy one and
sell one share of the stock), design an algorithm to find the maximum profit.
8. Say you have an array for which the ith element is the price of a given stock
on day i.
Design an algorithm to find the maximum profit. You may complete at most two tra
nsactions.
9. Show the execution of the Longest Common Subsequence algorithm on the follow
ing two strings:
ABCABDCDBA
BACDDCAB
10. Given a single sequence of numbers, design an algorithm to find the longest
monotonically increasing subsequence. For example, in the sequence 23143758 the
longest monotonically increasing subsequence is 23 4 58 . What is the running ti
me of your algorithm?

11. Given two sequences S1 and S2, design and analyze an algorithm to find the l
ongest common substring between S1 and S2 (a substring is a subsequence which ca
nnot skip intermediate characters, e.g. CABD is a substring of ABCABDCDBA).
12. Compute the Prefix function by the KMP algorithm for the pattern: A B C A B
D C D B A
13. Give an example of a string and a pattern which represents the best case for t
he KMP algorithm, i.e. the algorithm will run in the fastest possible manner for
such a problem instance
14. Find the contiguous subarray within an array (containing at least one number
) which has the largest sum.
For example, given the array [2,1,3,4,1,2,1,5,4],
the contiguous subarray [4,1,2,1] has the largest sum = 6.
15. Given two words word1 and word2, find the minimum number of steps required t
o convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character

You might also like