0% found this document useful (0 votes)
69 views4 pages

Solution DSA Midterm Fall 2014: Statement Will Not Deduct Any Marks, Don't Write More Than Space Provided

The document contains a multi-part coding question from a data structures and algorithms exam. Part 1 asks the student to identify correct and incorrect statements about algorithm time complexities and array/list operations, and output results of code snippets. Part 2 asks the student to write code for several tasks: calculating time complexity of a nested loop; defining functions for a BigInteger class; counting matching digits in two BigIntegers using a stack; and rearranging links in a doubly linked list to produce a given output. Part 3 asks the student to identify logical errors in code snippets without providing corrections, such as an error in printing repeating elements from an array and adding a node to the end of a singly

Uploaded by

Sadiq Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

Solution DSA Midterm Fall 2014: Statement Will Not Deduct Any Marks, Don't Write More Than Space Provided

The document contains a multi-part coding question from a data structures and algorithms exam. Part 1 asks the student to identify correct and incorrect statements about algorithm time complexities and array/list operations, and output results of code snippets. Part 2 asks the student to write code for several tasks: calculating time complexity of a nested loop; defining functions for a BigInteger class; counting matching digits in two BigIntegers using a stack; and rearranging links in a doubly linked list to produce a given output. Part 3 asks the student to identify logical errors in code snippets without providing corrections, such as an error in printing repeating elements from an array and adding a node to the end of a singly

Uploaded by

Sadiq Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DSA BIT + BSE-Aft F13

Solution DSA Midterm Fall 2014


Q1. [Correction + Output + Errors ] (15+15+5=35 Points)
a. If following statement(s) are incorrect, give correct statement, otherwise just write C for Correct?
Note: Writing C for an incorrect statement will deduct 0.25 marks, whereas writing correct statement for a correct
statement will not deduct any marks, don’t write more than space provided
Sample Statement: a[1] access second element of array
int a[]={...};
a[1] access first element of array
i. 3𝑛2 + 2𝑛 + 1 𝑖𝑠 𝛺(𝑛2 ) C
𝑛2 +7𝑛 𝑶(𝒏𝟐 )
ii. 𝑖𝑠 𝜃(𝑛2 )
√𝑛
3
C
iii. 2𝑛2 − 5𝑛 + 1 𝑖𝑠 𝛺 (𝑛2 )
iv. 2𝑛 (𝑛 + 3)𝑖𝑠 𝛺(2𝑛 ) C
v. 2𝑛 (𝑛 + 3)𝑖𝑠 𝛺(𝑛!) 𝑶(𝒏!)
vi. for (i=0;i<=n;i++), here condition statement n+2 times
executes n times
vii. for (i=0;i<=n;i++) 𝜽(𝒏) 𝒃𝒆𝒄𝒂𝒖𝒔𝒆 𝟏𝟎
for (j=0;j<=10;j++) ∗ 𝒏 𝒊𝒔 𝒏 𝒂𝒔𝒚𝒎𝒑𝒕𝒐𝒕𝒊𝒄𝒂𝒍𝒍𝒚
cout<<j; has complexity 𝜃(𝑛2 )
viii.Accessing ith element in linked list has worst case time 𝜽(𝒏), 𝒄𝒐𝒏𝒔𝒊𝒅𝒆𝒓𝒊𝒏𝒈 𝒍𝒊𝒔𝒕 𝒉𝒂𝒔 𝒏 𝒏𝒐𝒅𝒆𝒔
complexity 𝜃(1)
ix. In single circular list if pointer is pointing to ith node 𝜽(𝒏) 𝒐𝒓 𝜴(𝟏)
where i>1. Time complexity is 𝜃(1)
x. for (i=1;i<=n;i++) 𝜽(𝒏𝟐 ) 𝒐𝒓 𝜴(𝒏)
for (j=i;j<=i;j++)
cout<<j; has complexity 𝜃(𝑛)
xi. In single circular list deletion of last node has 𝜽(𝒏) 𝒐𝒓 𝜴(𝟏)
complexity 𝜃(1)
xii. In doubly circular list deletion of last node has C
complexity 𝜃(1)
xiii.In array based implementation of Stack some functions All functions are 𝜽(𝟏)
has complexity 𝜃(𝑛)
xiv. In array based implementation of Queue (Non Circular) C
some operations has complexity 𝜃(𝑛)
xv. Evaluation of Infix expression having n operands have C
complexity 𝜃(𝑛2 )

PUCIT-NC Resoure Person: Abdul Mateen Page 1 of 4


DSA BIT + BSE-Aft F13
b. Write output of the given codes:
int a[]={3,7,-1,4,1,8,-2};//Use array for part a-c Output:
a) 3-7-
7-
a) for (i=0;i<7;cout<<'\n',i++)
4-1-8-
for (j=i;a[j]>0;j++) cout<<a[j]<<'-'; 1-8-
8-

b) for (i=0;i<7;i++)//consider q is object of Queue b) 3-7-4-1-8--1--2-


if (a[i]>0) cout<<a[i]<<'-';
else q.enque(a[i]);
while (!q.isEmpty()) cout<<q.dequeue()<<'-';
c) 8-1-4-
c) for (i=0;i<7;i++) s.push(a[i];//consider s is Stack 7-3-
while (!s.isEmpty())
if (s.seeTop()>0) cout<<s.pop()<<'-';
else { cout<<'\n'; s.pop();} d) 8-6-3-1-8
d) first->2->3->6->8->1->x [Singly list without header]
temp=first->next;first->next=temp->next->next;
first->next->next->next=temp->next;
cout<<first->next->data<<'-';
cout<<temp->next->data<<'-'<<temp->data;
cout<<'-'<<first->next->next->data<<'-';
cout<<temp->next->next->data;
//consider doubly e) 1-8-2-1
h<->2<->3<->6<->8<->1
header circular
list
e) cout<<h->prev->data<<'-';
cout<<h->prev->prev->data<<'-';
cout<<h->next->next->prev->data<<'-';
cout<<h->prev->prev->next->data;
c. Find Logical errors in given codes, writing only error with reason, don’t write any code or avoid providing
correction: (1+2+2=5 Points)
Write errors with reasons:
a) for (i=0;i<10;i++)//Print repeating elements a) a[0] will check only repetition of first element, for all
if (a[i]==a[0]) cout<<a[i]; repetitions nested loop required
b) //Code to add at end for Singly Linear List b) After loop t will become null, t->next will be null
for (t=start;t!=null;t=t->next); pointer exception in second line
t->next=new Node(d);
end=t;
c) //Code to add at start for doubly header circular lis
DNode *t =new DNode(d); c) In third line t->prev will point to last node instead of
t->next=head->next; head node, in second last line head->next is t, so in
t->prev=head->prev; last line t->prev=t will create a self-loop
head->next=t;
head->next->prev=t;

PUCIT-NC Resoure Person: Abdul Mateen Page 2 of 4


DSA BIT + BSE-Aft F13
Q2. Write codes according to description given in each part: (5+4+5+5+3+5+8=35 Points)
a. Compute time complexity, also find Big O?

for (i=1;i<n;i=i+3){ n/3+1

a=0; n/3 j=1 5 25... n


where 1=50
for (j=n;j>=1;j=j/5) n/3(lgn+2) n=5lgnwhere lg
base is 5 from
a=a+j; n/3(lgn+1) 0 to lgn means
lgn+1 for true
cout<<a<<'-'; n/3
}
Total Time Complexity: 𝟐 𝟔
𝒏𝒍𝒈𝒏 + 𝒏 + 𝟏
𝟑 𝟑

Find Big O here:


𝟐 𝟔 𝟐 𝟐 𝟏𝟏
𝒏𝒍𝒈𝒏 + 𝒏 + 𝟏 = 𝒏𝒍𝒈𝒏 + 𝟐𝒏 + 𝟏 ≤ 𝒏𝒍𝒈𝒏 + 𝟐𝒏𝒍𝒈𝒏 + 𝒏𝒍𝒈𝒏 = 𝒏𝒍𝒈𝒏 → 𝑶(𝒏𝒍𝒈𝒏)
𝟑 𝟑 𝟑 𝟑 𝟑
b. Write a function for class BigInteger (reference Lab c. Write a function for class BigInteger using Stack int
task) bool isEven() countSame(BigInteger &b) to count and return how
many corresponding digits are same?
Node *t;
Node *t;
for (t=head->next;t->next!=null;t=t->next);
for(t=head->n;t!=null;t=t->n) s1.push(t->d);
return (!t->data%2);
for(t=b.head->n;t!=null;t=t->n) s2.push(t-
>d);

int count=0;

while (!s1.isEmpty() && !s2.isEmpty())

if (s1.pop()==s2.pop()) count++;

return count;

d. Consider doubly linear list, set links to produce given


Write code for part (b) here:
output, the forward output is starting from first and
going till null and reverse output is starting from last and first->next=last->prev; //8 & 1
going till null? last->next=last->prev->prev->prev; //3 & 6
x<-first->2<->3<->6<->8<->1<-last->x last->prev->prev->next=null;//termination
first->prev is pointing to null and last->next is
pointing to null. first is pointing to 2 means no header. last=last->prev;//8
Forward: 2 8 1 3 6 last->prev=first->next->next;//1
Reverse: 8 1 6 2 3 last->prev->prev=first->next->next->next->next;//6
last->prev->prev->prev=first;//2
Write like F->n->n=L->p->p->p [this is just example] first->prev=first->next->next->next;//3
first->next->next->next->prev=null;//termination

PUCIT-NC Resoure Person: Abdul Mateen Page 3 of 4


DSA BIT + BSE-Aft F13
e. Mathematically show that log base does not matter in asymptotic complexity?

𝒍𝒈𝒃 𝒏
𝒍𝒈𝒂 𝒏 = 𝒘𝒉𝒆𝒓𝒆 𝒍𝒈𝒃 𝒂 𝒊𝒔 𝒄𝒐𝒏𝒔𝒕𝒂𝒏𝒕, 𝒉𝒆𝒏𝒄𝒆 𝒍𝒈𝒂 𝒏 = 𝒄𝒍𝒈𝒃 𝒏, 𝒎𝒆𝒂𝒏𝒔 𝒕𝒉𝒆𝒚 𝒂𝒓𝒆 𝒂𝒔𝒚𝒎𝒑𝒕𝒐𝒕𝒊𝒄𝒂𝒍𝒍𝒚 𝒆𝒒𝒖𝒂𝒍,
𝒍𝒈𝒃 𝒂
𝒕𝒉𝒆𝒓𝒆𝒇𝒐𝒓𝒆 𝒍𝒐𝒈 𝒃𝒂𝒔𝒆 𝒅𝒐𝒆𝒔 𝒏𝒐𝒕 𝒎𝒂𝒕𝒕𝒆𝒓 𝒊𝒏 𝒂𝒔𝒚𝒎𝒑𝒕𝒐𝒕𝒊𝒄 𝒄𝒐𝒎𝒑𝒍𝒆𝒙𝒊𝒕𝒚

f. For doubly header circular linked list write member function to reverse list by swapping data, assume swap(Node
*t1, Node *t2) function is given? [To secure full marks ensure minimum steps], count time complexity of your
code?
for (t1=head->next, t2=head->prev;t1!=t2 && t1->prev!=t2;t1=t1->next, t2=t2->next)

swap(t1, t2);

First step has time n/2+1; whereas next line will executes n/2 times, hence total time is
n+1.

g. Given BigInteger is implemented using doubly header circular linked list, write function (without using stack) bool
has (BigInteger &b) return true if first integer is same to second or first integer is larger and has same digits on
corresponding position. Define best case & worst case and compute time complexity for each?
Best Case: Mismatch in first digit
Worst Case: No mismatch and both has n digits Best Case Worst Case
DNode *t1, *t2; 1 1
for (t1=head->p, t2=b.head->p;t1!=head && t2!=b.head;) 1 n+1
{ n
if (t1->data!=t2->data) return false; 1 n
t1=t1->prev;t2=t2->prev;
}
if (t1!=head) return true; 1
else return false;
______________ ______________
3
3n+3

PUCIT-NC Resoure Person: Abdul Mateen Page 4 of 4

You might also like