You are on page 1of 5

YEDITEPE UNIVERSITY

Department of Information and Computer Science


ICS 211 -- Data Structures
SAMPLE QUESTIONS
1. __________ is the dereferencing operator in C++.
2. __________ is the address of operator in C++.
3. __________ is the indirection operator in C++.
Assume the following declarations:
RationalNumber *i, j = RationalNumber(12,53);
Answer each of the questions 4-10 with (a) an address OR (b) a RationalNumber value
4. The value of i will be ________
5. The value of &j will be ________
6. The value of j will be ________
7. The value of &i will be ________
8. The value of *i will be ________
9. The value of *i*j will be ________
10. The value of (*i)*j will be ________

11. Given the RPN, 2345-* +, answer the following questions:


a) What is the corresponding infix notation?
b) Assume for evaluating the above RPN you make use of a stack, what are the stack calls
made? Show the contents of the stack after each call is made.
REMEMBER: The first poped number becomes the second argument ( x1 binOp x2 ) to a
binary operation.
12. Given a digraph, G=<V,E> with vertex set V={1,2,3,4,5} and edge set
E={<1,1>,<1,2>,<1,3>,<1,4>,<2,1>,<2,2>,<2,5>,<3,3>,<3,5>,<4,4>,<5,3>,<5,5>}
Answer the following questions:
a. Draw the digraph,
b. Show its adjacency matrix form,
c. Show its adjacency list form,
d. Which form (b.,c.) is advantages for this graph and why?
e. Indicate whether or not the graph has the following properties and why:
• symmetric, transitive, acyclic
13. One kind of sparse matrix that arises often in numerical analysis is the tridiagonal matrix for
which the LU decomposition can be formed without pivoting. This decomposition will yield a
sparse square matrix, which has 1’s on the major diagonal and nonzero elements on the
diagonal immediately above and below it (superdiagonal). Assume M is a special type of a
tridiagonal matrix and it is symmetric, as shown below:
M=

1 a1 0 … 0
a1 1 a2 0 .
0 a2 .
. .
. 1 an-1
0 … 0… an-1 1
nxn
If the elements of this array are represented row-wise by means of an array, A, e.g., M(1,1) is
stored at A(1), obtain a storage mapping function to determine the value of M(i,j), 1≤i,j≤n
from the array A.
14. A Robot can move in a 2D space where obstacles, such as walls exist, one step at a time. Each
step consists of moving one unit of distance towards UP, DOWN, RIGHT or LEFT. A valid
step of the Robot consists of taking a step without colliding an obstacle. In order to start the
operation of the Robot, user must put the Robot at some initial location (xi,yi) and turn it on.
Given a destination location, (xd,yd) Robot will be able to move from its current position to
the destination. Also Robot can be asked to return back to its initial location. Define an
abstract class for Robot, state the possible attributes and the methods for this class. Explain.
15. Assume A[] is an array of random real numbers of size LENGTH. Write a recursive
algorithm to find the minimum value in A[]. Analyze your algorithm. How many recursions
will occur? What is the running time of your algorithm?
16. Given the following definition of a doubly linked list class:
class DoublyLinkedList{
Node* first;
class Node{
float data;
Node* next;
Node* prev;
}
void deleteNode( Node* X ) { };
}

Let first be a pointer to the first node in a DoublyLinkedList object and X is the address of an
arbitrary node in the list. Write a member function, deleteNode to delete this node from the list.
Hint: Consider the case X points to the same locaiton as first.
17. Consider a language that does not have arrays but have linked lists as a data type. That is, one
can declare
LinkedList l;
and the following operations are defined.
template <class object>
class LinkedList {
// inserts an element from the head of the list
void insertFromHead( object );
// removes an element from the head of the list and returns the value removed
object deleteFromHead();
// returns true if the linked list has no elements
bool isEmpty();
// returns the object pointed by the head without removing it
object head();
}
Show how a one-dimensional integer array can be implemented by using above operations on
two linked lists of integers:
class Array{
LinkedList<int> *l1;
LinkedList<int> *l2;
int maxSize;
// creates an array of size, maxsize with initial values of 0s, e.g. int arr[maxSize]
void createArray(int maxsize) { };
// returns the value of ith element in the array, e.g. arr[i]
int getIthElement(int i) { };
// sets the value of ith element, e.g. arr[i] = value
int setIthElement( int i, int value ) { };

}
Write C++ codes for createArray, getIthElement, setIthElement methods using l1 and l2. You can
safely assume that codes of constructors and destructors for Array and LinkedList exist and make
necessary allocations and deallocations.
18. Given a randomly generated table, Table[1..n,1..m], write an efficient algorithm that
arranges the values in this nxm table (two dimensional array) such that:
Table[i,j] ≤ Table[i,j+1]
Table[i,j] ≤ Table[i+1,j]
That is, this table is sorted along the rows and columns. Analyze your algorithm.
19. MYSTERY(A) {
for ( k=1; k<=length[A]; k++) {
Key = A[k];
Index = k;
for (j=k+1; j<=length[A]; j++) {
If A[j]<Key Then {
Key = A[j];
Index=j;
}
}
Swap(A[k], A[Index]); // swap the contents of A[k] and A[Index]
}
} // end of MYSTERY

a. Apply the above algorithm to the array A={3,56,45,12,132,23}. Name this algorithm:
Simple Selection, Delayed Selection, Linear Insertion, Binary Insertion.
b. Analyze this algorithm, given that length[A]=n.
20. The multiplication method for creating hash functions operates in two steps. First we multiply
the key k by a constant A in the range (0,1) and extract the fractional part of kA. Then we
multiply this value by m and take the floor of the result. In short, the hash function is h(k)= 
m( (k A) mod 1) , where ( (k A) mod 1) means the fractional part of kA. Assume collisions are
resolved using bucket hashing, show the contents of the hash-table with a size m=5 (indices
change from 0 to 4) using h(k) as the hash function with A=0.51, after inserting the keys
10,8,2,70,30,60 respectively.
21. Draw two BSTs of depth 2 and depth 5, respectively containing the values {A, D, L, M, Y,
Z}.
22. For each application below, say what data structure(s) would be most appropriate for the
primary task and why, i.e. why is the data structure better suited than other possibilities. An
answer without the explanation is worth nothing.
a. You need to implement a calculator that reads postfix expressions and returns their values.
b. When you buy a ticket in the State Lottery, you choose six different numbers between 1
and 50. Thousands of tickets are sold. The lottery officials keep a dictionary on each ticket
sold keyed on the set of six numbers chosen on each ticket. After the officials pick the
winning numbers, they access this dictionary to identify the winning ticket or tickets, if
any.
c. Assume you are spying and listening to a port of a specific computer. Whenever some
data is sent using this port of the computer to outside, you store a copy of it into your
computer. The problem is there is no way of knowing how much data there might be. You
must store it in a predictable (constant) amount of time. You can safely assume you can
distinguish between each transfer, but you have no idea how much data will be sent
during each communication.
d. A CD writer is shared by multiple users. You want to schedule the requests from users for
accessing it.
e. A palindrome is a string that reads backwards the same as forwards, e.g., “radar” and
“kavak”. By comparing a string with its reverse, you can understand whether it’s a
palindrome or not.
23. Write a C++ code for an additional member function
void stackCopy( Stack s ) , which copies all the items in s onto a Stack object (without
changing the order), for a Stack class defined as below:
template <class myType>
class Stack {
void push( myType obj ): pushes an object (of myType) onto the top of stk
myType peek(): looks at the object at the top of this stack without removing it from the stack.
void pop(): removes the object at the top of this stack
int Isempty(): returns 1 if the stack is empty, 0 otherwise
}
Hint: You can use other temporary variables.
24. Given the following BST tree,
a) What is the resulting tree, G’ after applying following operations Insert(“C”), Insert (“T”),
Delete(“A”), Delete(“K”), Delete(“E”), Delete(“R”) consecutively ?
b) Show inorder, preorder and postorder traversal output after applying the related algorithm to
the resulting tree, G’. Assume visiting a node consists of displaying the corresponding
content of the node.

G: D a) resulting tree, G’

B F

A E R

K S
Inorder :
Preorder :
Postorder : L
25. Given the following definition of a LinkedList class, what are the uses of the member
functions mystery1 and mystery2 (assuming no error check)? In other words, assuming A, B
and C are pointers to LinkedList objects, what would A->mystery1() and B->mystery2(C)
calls do on A, B and C?
class LinkedList{

class Node{

int data;
Node* next;
}
Node* first;
int size;
void deleteNode( Node *); // deletes the node supplied as an arg.
Node *getFirst(); // returns a pointer to the head of the list (returns first)
int getSize(); // returns the size
void mystery1() { void mystery2(LinkedList* Y){
Node* p,q,r; Node* p;
p = first; size += Y->getSize();
q = null; if ( first == null ) {
first = Y->getFirst();
while (p!=null){ return;
}
r = q; if ( Y== null) return;
q = p; p = first;
p = p->next; while (p->next != null) p = p-
>next;
} p->next=Y;
first=q; }
}
} // end of class definition
26. For each of the following program segments make a running time analysis and state your
results using big-oh notation and in terms of N.
a. for (int i = 0; i<2*N; i++)
total++; O( )
b. for (int i = 0; i<N; i++)
for (int j = N; j>0; j--) O( )
total++;

You might also like