Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Recursive Function - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Recursive Function, Object Oriented, Processor Speed, Data Size, Recursive Function, Internal Node, Two Pointers

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt 🇮🇳

4.3

(8)

159 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
15-111 Introductions to Data Structures
Summer II - 09
Midterm Exam – 80 minutes
Instructions: You are allowed to bring one page of notes. You will have access to APIs’. No other material
allowed. Please write your answers clearly and legibly to receive full credit.
Name: ____________________________________________ (please print)
1. Find the best answer for the following multiple choice questions. (20 points)
a. Java is
i. Procedural ii. Object oriented iii. Functional iv. None of the above
b. A Java interface is
i. Extended by a class
ii. Implemented by a class
iii. Contains methods that are already implemented
iv. None of the above
c. The runtime of an algorithm depends on
i. Data size
ii. Processor speed
iii. Language
iv. RAM
v. All of the above
vi. None of the above
d. Inserting an element to the beginning of an array (that is A[0] element) is more difficult
than inserting an element to the beginning of a linked list.
i. TRUE
ii. FALSE
e. Suppose Node is a class that contains two integer fields and a pointer to next Node.
Consider the following definitions Node M = new Node(); Node N = M; The number of
bytes required to hold M and N are
i. M = 4 bytes, N = 8 bytes
ii. M = 8 bytes, N = 4 bytes
iii. M = 12 bytes, N = 4 bytes
iv. M = 4 bytes, N = 12 bytes
v. None of the above
pf3
pf4
pf5
pf8

Partial preview of the text

Download Recursive Function - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

15-111 Introductions to Data Structures

Summer II - 09

Midterm Exam – 80 minutes

Instructions: You are allowed to bring one page of notes. You will have access to APIs’. No other material allowed. Please write your answers clearly and legibly to receive full credit.

Name: ____________________________________________ (please print)

  1. Find the best answer for the following multiple choice questions. (20 points) a. Java is i. Procedural ii. Object oriented iii. Functional iv. None of the above b. A Java interface is i. Extended by a class ii. Implemented by a class iii. Contains methods that are already implemented iv. None of the above c. The runtime of an algorithm depends on i. Data size ii. Processor speed iii. Language iv. RAM v. All of the above vi. None of the above d. Inserting an element to the beginning of an array (that is A[0] element) is more difficult than inserting an element to the beginning of a linked list. i. TRUE ii. FALSE e. Suppose Node is a class that contains two integer fields and a pointer to next Node. Consider the following definitions Node M = new Node(); Node N = M; The number of bytes required to hold M and N are i. M = 4 bytes, N = 8 bytes ii. M = 8 bytes, N = 4 bytes iii. M = 12 bytes, N = 4 bytes iv. M = 4 bytes, N = 12 bytes v. None of the above

f. Given a collection of algorithms that runs on O(1), O(n log n), O(n), O(n^2 ), O(log n), O(n!), order the algorithms from fastest to slowest i. O(1), O(n log n), O(n), O(n^2 ), O(log n), O(n!) ii. O(1), O(log n), O(n), O(n log n), O(n^2 ), O(n!) iii. O(1), O(log n), O(n log n), O(n), O(n^2 ), O(n!) iv. None of the above g. Suppose that the complexity of an algorithm is O(n^2 ). Suppose that the program that uses the algorithm run in 10 seconds for a data set of size n. If the data size is doubled, how long will it take (approximately) to run the program? i. 10 seconds ii. 100 seconds iii. 6-7 minutes iv. None of the above h. What is the best data structure to solve the following problem? A list needs to be built dynamically. Data must be easy to find, preferably in O(1). The user does not care about any order statistics such as finding max or min or median. i. Use an Array ii. Use a Singly LL iii. Use a Stack iv. Use a Queue v. None of the above i. Finding the max element in an unordered stack would require i. O(1) operations ii. O(log n) operations iii. O(n) operations. iv. None of the above j. Suppose that recursive function foo(n) calls itself twice within its body(assume foo(n-1) called twice). Also assume the recursion would end when n=0. How many function calls does foo initiates? i. O(n) ii. O(n^2 ) iii. O(2n) iv. O(n!) v. None of the above

  1. [15 points] Assume that LL is a DOUBLY linked list with the head node and at least one other internal node M which is not the last node. Write few lines of code to accomplish the following. You may assume that each node has a next pointer and prev pointer. You may NOT swap data to accomplish any of the following operations. For each operation, assume the original list as described above. You are encouraged to draw pictures to justify your code. Note that for each operation, you need to manipulate at least two pointers, next and prev.

a. Delete the head node

b. Insert a node P immediately after M

c. Swap head and the node M (you may not swap data)

  1. [10 points] Complete the following method in the LinkedList class. The method contains is supposed to return true if the there is a node in the list that is equal to the given Comparable c. You can assume the Node class has the public fields, data (a Comparable) and next (a pointer to another Node)

public boolean contains(Comparable c) {

  1. [10 points] Suppose you were asked to write a method that will take two sorted stacks A and B (min on top) and create one stack that is sorted (min on top). You are allowed to use only the stack operations such as pop, push, size and top. No other data structure such as arrays are not allowed. You are allowed to use stacks. Note that elements on the stack can be compared using compareTo.

Public Stack mergeSortedStacks(Stack A, Stack B) {

  1. [5 pts] Write a method findLast that will find the last node of a LL. Return a pointer (reference) to the last node

public Node findLast(LinkedList myLL){

  1. [10 pts] For each of the following scenarios choose the “best” data structure from the following list or a combination of data structures: an unsorted array, linked list, DLL, circular LL, stack, queue. In each case, justify your answer briefly.

a. Suppose that a grocery store decided that customers who come first will be served first

b. A list must be maintained so that any element can be accessed randomly

c. A program needs to remember operations it performed in opposite order

d. The size of a file is unknown. The entries need to be entered as they come in. Entries must be deleted when they are no longer needed. It is important that structure has flexible memory management

e. A list must be maintained so that elements can be added to the beginning or end in O(1)

Extra Credit: (10 pts) write a method createArrayOfLL that takes a file of words, one in each line and creates an array of linked list nodes as follows.

(a) Create an array of 26 linked lists (b) Open the file and Insert each word into the array based on its first letter, eg: any word that begins with ‘a’ goes to A[0] list etc.. (c) Return the reference to the array of linked lists

You must consider ALL cases. You must write reasonable comments to describe your algorithm. You may assume public fields for each node, data and next. There WILL NOT be any partial credit for this problem. The question will be graded ALL or NOTHING. [Use extra paper, if necessary to write the answer]. Node class is defined as follows.

Public Node[] createArrayOfLL(String inputfile) {