Title: EE441 Data Structures Example Questions
1EE441 Data StructuresExample Questions
- Özgür B. Akan
- Department of Electrical Electronics
Engineering - Middle East Technical University
- akan_at_eee.metu.edu.tr
- www.eee.metu.edu.tr/akan
2Example Questions
- Q1) Consider the following C program section
- int S(int a, int b)
- int t
- ta
- ab2
- bt2
- return t
- void main(void)
- int p
- int q, A4
- for (int i0 ilt4 i)
- Aii2
- pA2
- // control point 1
- qS(p,A0)
- // control point 2
- S(A0,p)
- // control point 3
- Show the contents of p, q and the array A at
- Control point 1
- Control point 2
- Control point 3
3Example Questions
- A1)
- int S(int a, int b)
- int t
- ta
- ab2
- bt2
- return t
- void main(void)
- int p
- int q, A4
- for (int i0 ilt4 i)
- Aii2
- pA2
- // control point 1
- qS(p,A0)
- // control point 2
- S(A0,p)
- // control point 3
- At Control point 2
- S(4 by value, 0) t4 local a020 A0?8
4Example Questions
- A1)
- int S(int a, int b)
- int t
- ta
- ab2
- bt2
- return t
- void main(void)
- int p
- int q, A4
- for (int i0 ilt4 i)
- Aii2
- pA2
- // control point 1
- qS(p,A0)
- // control point 2
- S(A0,p)
- // control point 3
(c) At Control point 3 t8 local
a428 // note that the 2nd parameter // of
function S is passed // by reference,
thus A2?8216
5Example Questions
- Q2)
- (a) What is the order (time complexity in O(.)
notation) of the following tasks in the worst
case? - i. computing the sum of the first n even
integers by using a for loop - ii. pushing an item to a stack of n items
- (b) Order the following functions by growth rate
nlog n, nloglog n, log2 n, nlog n2, 2n/2, 2n, 37,
n2log n, 2000. Indicate the group of functions
which grow at the same rate. - (c) Suppose that the implementation of a
particular algorithm appears in C as follows - for (int i1 igtn i)
- for (int j1 jgti j)
- for (int k1 kgt10 k)
- myfunction(i,j,k,n)
-
-
- What is the time complexity of the algorithm,
assuming that myfunction has a running time,
which is O(log n)? Justify your answer.
6Example Questions
- A2)
- (a) i. computing the sum of the first n even
integers by using a for loop - for (i1iltni) sumsumi2
- the work (time it takes) grows linearly with
n, hence O(n) - ii. pushing an item to a stack of n items
-
- Recall the implementation of Push method of
Stack class - void StackPush(const DataType item)
-
- if (topMaxStackSize-1)
-
- cerrltlt"Stack overflow"ltltendl exit(1)
-
- top
- stacklisttop item
-
- work done in pushing an item to a stack is
independent of stack sizeHence, O(1)
7Example Questions
- A2)
- (b) 37, 2000
- log2 n
- nloglog n
- nlog n, nlog n2
- n2log n
- 2n/2, 2n
- (c) If myfunction requires t time units, the
innermost loop on k requires 10t time units. The
loop of j requires 10ti time units and the
outermost loop on i requires
for (int i1 igtn i) for (int j1 jgti
j) for (int k1 kgt10 k) myfunction(i,
j,k,n)
Since t is O(log n) then the overall execution
time is O(n2log n)
8Example Questions
Q3) Consider the following C program
- 1- include ltiostream.hgt
- 2-
- 3- class MyClass
- 4- private
- 5- char c
- 6- public
- 7- MyClass(const int n)
- 8- char Put(const int n)
- 9- char Get(const int n)
- 10-
- 11-
- 12- MyClassMyClass(const int n)
- 13- c new charn
- 14-
- 15- char MyClassPut(const int n)
- 16- return cn
- 17-
- 18- char MyClassGet(const int n)
- 19- return cn
21- void MyFn(MyClass m1) 22- 23- MyClass
mc 24- 25- mc new MyClass(10) 26- mc-gtPut(3)
a 27- m1.Put(3) mc-gtGet(3) 28-
29- 30- main() 31- 32- int n 33- MyClass
m1(20) 34- 35- cin gtgt n 36- MyFn(m1) 37- cout
ltlt m1.Get(3) 38-
What is the major programming error related to
dynamic memory usage in this program? Indicate
how you would correct this error by giving the
line numbers of the lines you would delete, if
necessary, and adding new code, if necessary.
9Example Questions
A3) Destructor is missing because the class uses
dynamic memory to allocate memory space. This
space must be returned to the system memory
manager.
- 3- class MyClass
- 4- private
- 5- char c
- 6- public
- 7- MyClass(const int n)
- 8- char Put(const int n)
- 9- char Get(const int n)
- MyClass()
- 10-
- 12- MyClassMyClass()
- 13- delete c
10Example Questions
Q4) Given the following C program
void myfunction(int n) Stackltintgt SA, SB,
SC int prev, current SA.Push(1) for
(int i2 iltn i) coutltendl coutltlti
ltltiltlt previ SB.Push(prev) coutltltSB.Peek
ltlt, while !(SA.StackEmpty())
currentprev SA.Pop SB.Push(current)
coutltltSB.Peekltlt, prevcurrent
while !(SB.StackEmpty()) SC.Push(SB.Pop)
while !(SC.StackEmpty()) SA.Push(SC.Pop)
- include ltiostream.hgt
- template ltclass Tgt
- Class Stack
-
- private
- T stacklistMaxStackSize
- int top
- public
- Stack(void)
- void Push(const T item)
- T Pop(void)
- void Clearstack(void)
- T Peek(void) const
- int StackEmpty(void) const
- int StackFull(void) const
- What will be the output if myfunction is called
with n6, i.e., myfunction(6) ? - What is the complexity of the algorithm in O(.)
notation in terms of n?
11Example Questions
A4)
void myfunction(int n) Stackltintgt SA, SB,
SC int prev, current SA.Push(1) for
(int i2 iltn i) coutltendl coutltlti
ltltiltlt previ SB.Push(prev) coutltltSB.Peek
ltlt, while !(SA.StackEmpty())
currentprev SA.Pop SB.Push(current)
coutltltSB.Peekltlt, prevcurrent
while !(SB.StackEmpty()) SC.Push(SB.Pop)
while !(SC.StackEmpty()) SA.Push(SC.Pop)
(a) myfunction(6)
i22,3, i33,6,8, i44,12,18,21, i55,26,4
4,56,60 i66,66,122,166,192,197
(b) O(nn)
12Example Questions
- Q5) Give the final values of X, Y and A after the
following C statements are executed - void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
13Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
PA
2.3
4.5
8.9
1.0
5.5
3.5
4
PX
X
7
PY
Y
14Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
PA
2.3
4.5
8.9
1.0
5.5
3.5
3
PX
X
10
PY
Y
15Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
PA
5.3
4.5
8.9
1.0
5.5
3.5
55
PX
X
10
PY
Y
16Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
5.3
4.5
PA
8.9
1.0
5.5
3.5
55
PX
X
10
PY
Y
17Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
5.3
6.8
8.9
PA
1.0
5.5
3.5
55
PX
X
10
PY
Y
18Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
5.3
6.8
8.9
1.0
PA
5.5
3.5
55
PX
X
10
PY
Y
19Example Questions
- A5)
- void main()
-
- int X4, Y7, PXX, PY
- float A2.3, 4.5, 8.9, 1.0, 5.5, 3.5,
PAA0 - PYY
- (PX)--
- PYPX
- PYPX
- PY55
- PA3.0
- PA
- PA6.8
- PA2
- PA3.3
5.3
6.8
8.9
1.0
5.5
PA
3.3
55
PX
X
10
PY
Y
20Example Questions
Q6) Find the contents of a, b, c, d, e at
checkpoints
- void main()
-
- int a3//an array of pointers to integers
- int b 10,11,12,13//an array of integers, b
points at the element 0 - int c 20,21,22,23//an array of integers, c
points at the element 0 - int d200
- int e300
- a0d
- a1c2
- a2b
- //checkpoint 1
- Myfunction (e, (a1),a, a)
- //checkpoint 3
- (a1)4
- a1
- //checkpoint 4
void Myfunction (int s, int x, int y, int
z) (z11)5 x y ys x
100 //checkpoint 2
21Example Questions
- A6)
- void main()
-
- int a3//an array of pointers to integers
- int b 10,11,12,13//an array of integers, b
points at the element 0 - int c 20,21,22,23//an array of integers, c
points at the element 0 - int d200
- int e300
- ...
b
c
10
20
11
21
12
22
13
23
Array of integers
Array of integers
22Example Questions
- A6)
- void main()
-
- int a3//an array of pointers to integers
- int b10,11,12,13//an array of integers, b
points at the element 0 - int c20,21,22,23//an array of integers, c
points at the element 0 - int d200
- int e300
- a0d
- a1c2
- a2b
a
Array of pointers to integers
b
c
10
20
11
21
12
22
13
23
Array of integers
Array of integers
Checkpoint 1
23Example Questions
- Function call
- Myfunction (e, (a1),a, a)
- (z11)5//first statement, pass by address
- A6)
- void Myfunction (int s, int x, int y, int
z) -
- (z11)5
- x y
- ys
- x 100
- //checkpoint 2
-
- Takes an integer variable s and might change its
value (integer) - Takes a pointer to an integer x
- Takes a pointer to an integer variable y and
might change its value (pointer to an integer,
might change what it points at) - Takes a pointer to a pointer to an integer z and
might change the value of its contents (pointer
to an integer, might change what it points at)
a
y
Array of pointers to integers
xz1
b
c
10
20
11
21
12
22
13
5
Array of integers
Array of integers
24Example Questions
- Function call
- Myfunction (e, (a1),a, a)
- x y//second statement, pass by value
- A6)
- void Myfunction (int s, int x, int y, int
z) -
- (z11)5
- x y
- ys
- x 100
- //checkpoint 2
-
- Takes an integer variable s and might change its
value (integer) - Takes a pointer to an integer x
- Takes a pointer to an integer variable y and
might change its value (pointer to an integer,
might change what it points at) - Takes a pointer to a pointer to an integer z and
might change the value of its contents (pointer
to an integer, might change what it points at)
y
a
x
Array of pointers to integers
b
c
10
20
11
21
12
22
13
5
Array of integers
Array of integers
25Example Questions
- Function call
- Myfunction (e, (a1),a, a)
- y s//third statement, pass by reference
- A6)
- void Myfunction (int s, int x, int y, int
z) -
- (z11)5
- x y
- ys
- x 100
- //checkpoint 2
-
- Takes an integer variable s and might change its
value (integer) - Takes a pointer to an integer x
- Takes a pointer to an integer variable y and
might change its value (pointer to an integer,
might change what it points at) - Takes a pointer to a pointer to an integer z and
might change the value of its contents (pointer
to an integer, might change what it points at)
a
x
Array of pointers to integers
y
b
c
10
20
11
21
12
22
13
5
Array of integers
Array of integers
26Example Questions
- Function call
- Myfunction (e, (a1),a, a)
- x 100//fourth statement, pass by address
- A6)
- void Myfunction (int s, int x, int y, int
z) -
- (z11)5
- x y
- ys
- x 100
- //checkpoint 2
-
- Takes an integer variable s and might change its
value (integer) - Takes a pointer to an integer x
- Takes a pointer to an integer variable y and
might change its value (pointer to an integer,
might change what it points at) - Takes a pointer to a pointer to an integer z and
might change the value of its contents (pointer
to an integer, might change what it points at)
a
x
Array of pointers to integers
y
b
c
10
20
11
21
12
22
13
5
Array of integers
Array of integers
Checkpoint 2
27Example Questions
- Exit function
- Myfunction (e, (a1),a, a)
- A6)
- void Myfunction (int s, int x, int y, int
z) -
- (z11)5
- x y
- ys
- x 100
- //checkpoint 2
-
- Takes an integer variable s and might change its
value (integer) - Takes a pointer to an integer x
- Takes a pointer to an integer variable y and
might change its value (pointer to an integer,
might change what it points at) - Takes a pointer to a pointer to an integer z and
might change the value of its contents (pointer
to an integer, might change what it points at)
a
Array of pointers to integers
x(a1)
b
c
10
20
11
21
12
22
13
5
Array of integers
Array of integers
Checkpoint 3
28Example Questions
a
- After function call
- Myfunction (e, (a1),a, a)
Array of pointers to integers
x(a1)
b
c
10
20
11
21
12
4
13
5
Array of integers
Array of integers
Checkpoint 4
29Example Questions
- Q7) Given the partial class definition
class MyClass private float a50
public MyClass(void) ??? Fun(???
J) // single input argument
Implement the member function Fun which will
allow the user to insert floating numbers to the
desired location in the floating array a. Array
indices greater than 49 will be inserted in the
last location and negative indices will be
inserted in the 1st location in the array. Any
index between 0-49 will be inserted in the
corresponding location in the array. This member
function will take only a SINGLE input argument.
Comment on each line of your code
30Example Questions
- A7) In this question, your function should be
able to put a floating number x in the array
location y - float MyClassFun(int j)
-
- int index
- indexj
- if (jlt0)
- index0
- if (jgt49)
- index49
- return aindex
-
- An example use of this function in a main program
is - MyClass A
- int x10
- float y 1.234
- A.Fun(x) y
31Example Questions
- Q8) Given the main program and function Fun
void Fun(int a, int b) aa5
ba-2 main () int a50, b50 int
p, q pa3 qb5 Fun(p,q) //
Line (X)
a) What are the contents of p and q after the
execution of line (X)? b) Propose a way how Fun
should be modified in order to make p point to
the first element in a and q point to the first
element in b. Also indicate how Fun should be
called in line(X)
32Example Questions
void Fun(int a, int b) aa5
ba-2 main () int a50, b50 int
p, q pa3 qb5 Fun(p,q) //
Line (X)
a) Since the parameters are passed by value, the
contents of p and q are unchanged.
33Example Questions
b) Propose a way how Fun should be modified in
order to make p point to the first element in a
and q point to the first element in b. Also
indicate how Fun should be called in line(X)
void Fun(int a, int b) aa-3
bb-5 main () int a50, b50 int
p, q pa3 qb5 Fun(p,q) //
Line (X)
void Fun(int a, int b) aa-3
bb-5 main () int a50, b50
int p, q pa3 qb5 Fun(p,q)
// Line (X)
34Example Questions
- Q9) (a) For the following declaration of the
Stack class, assuming that the public methods
Stack, Push, Pop, StackEmpty and StackFull are
implemented as discussed in class, implement the
C member function Count that will return the
number of items in the owner object. Your member
function should access the data structure through
the existing member functions.
template ltclass Tgt class Stack private T
stacklistMaxstacksize int
top public Stack(void) void Push(const T
item) T Pop(void) int StackEmpty(void)
const int StackFull(void) const int
Count(void) const
35Example Questions
- Q9) (b) Using the Stack class definition in part
(a), implement a global function - template ltclass Tgt
- int Bottom (StackltTgt s, T last)
-
- that returns 0 if stack s is empty and 1 if it
is non-empty, assigning the bottom element to the
argument last. Upon return from your function the
original stack should remain unchanged. Your
function implementation should be properly
commented for understandability. -
36Example Questions
- Q9) (c) Still using the Stack class definition in
part (a), what is the output from the following
sequence of operations? - Stackltintgt S
- int x3, y5, z7
- s.Push(8)
- s.Push(x)
- s.Push(y)
- s.Push(z)
- xs.Pop()
- s.Pop()
- s.Push(4)
- coutltlt x ltlt endl // output1 .
- coutltlt s.Count()ltltendl // output2 .
- ys.Pop()
- zs.Pop()
- coutltlt y ltlt endl // output3 .
- While (!s.StackEmpty())
- xs.Pop()
- coutltltxltltendl // output4 .
37Example Questions
- A9) (a) (with the ability of member function to
access private data members) - template ltclass Tgt
- int StackltTgtCount(void) const
- return (top1)
- (b)
- template ltclass Tgt
- int Bottom(StackltTgt s, T last)
- StackltTgt temp_s // temp_s will store the
original stack in reverse order - // first reverse original stack into temp_s
- while(!s.StackEmpty())
- temp_s.Push(s.Pop())
- // now, bottom of original is top of temp_s
- lasttemp_s.Pop() // this is the wanted item
- s.Push(last) // restore bottom
- while (!temp_s.StackEmpty()) // restore the
rest - s.Push(temp_s.Pop())
- return
-
38Example Questions
- A9) (c)
- Stackltintgt S
- int x3, y5, z7
- s.Push(8)
- s.Push(x)
- s.Push(y)
- s.Push(z)
- xs.Pop()
- s.Pop()
- s.Push(4)
- coutltlt x ltlt endl // output1 7
- coutltlt s.Count()ltltendl // output2 3
- ys.Pop()
- zs.Pop()
- coutltlt y ltlt endl // output3 4
- While (!s.StackEmpty())
- xs.Pop()
- coutltltxltltendl // output4 8
7
5
3
8
39Example Questions
- Q10) Consider the following C class
declaration - class Z
- private
- int z1 int z2
- public
- void Z(const int x1, x2)
- void Z(const Z x)
- int first (void) return z1
- int second (void) return z2
-
- (a) Assuming that a complete implementation of
this class, exactly as it is declared here, is
available, draw the constructed data structures
after the following program sequence is executed -
- Z zp
- zp new Z(3,5)
- Z a(6, (zp-gtfirst() ) ), ba, c(0,0)
- c zp
- delete zp
-
- (b) Give an appropriate implementation for the
destructor function for this class
40Example Questions
- A10) class Z
- private
- int z1 int z2
- public
- void Z(const int x1, x2)
- void Z(const Z x)
- int first (void) return z1
- int second (void) return z2
-
- (a)
- Z zp
- zp new Z(3,5)
- Z a(6, (zp-gtfirst() ) ), ba, c(0,0)
- c zp
- delete zp
-
deleted
zp
3
5
41Example Questions
- A10) (b) ZZ(void)
- delete z1 delete z2