• 沒有找到結果。

Midterm Examination Problem Sheet

N/A
N/A
Protected

Academic year: 2022

Share "Midterm Examination Problem Sheet"

Copied!
2
0
0

加載中.... (立即查看全文)

全文

(1)

Data Structures and Algorithms (NTU, Spring 2015) instructor: Roger Jang, Hsuan-Tien Lin

Midterm Examination Problem Sheet

TIME: 04/21/2015, 14:20–16:20

This is a open-book exam. You can use any printed materials as your reference during the exam. Any electronic devices are not allowed.

Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts.

Both English and Chinese (if suited) are allowed for answering the questions. We do not accept any other languages.

There are 5 problems in the exam, each worth 40 points—the full credit is 200 points. Some problems come with two sub-problems to help you get partial credits, making a total of 9 sub-problems. For the 9 problems, 3 of them are marked with * and are supposedly simple; 4 of them are marked with ** and are supposedly regular; 2 of them are marked with *** and are supposedly difficult.

1. C and C++

(a) (20%, *) Give a concrete example in C/C++ to explain in your own words on how memory leak could happen.

(b) (20%, *) Suppose we use the following class to represent the homework scores of a student in DSA class:

1 c l a s s myScore {

2 public :

3 myScore ( i n t n = 1 0 ) { s i z e = n ; hwScore = new i n t [ s i z e ] ; }

4 ˜ myScore ( ) { delete [ ] hwScore ; }

5 s t r i n g name ;

6 i n t ∗ hwScore ;

7 } ;

Draw the memory layout after running the following code snippet, and explain the potential hazards/problems of the code.

1 myScore a ( 6 ) ;

2 a . name = ” John ” ; a . hwScore [ 0 ] = 8 5 ; a . hwScore [ 1 ] = 9 0 ;

3 myScore b = a ;

4 b . name = ”Mary” ; b . hwScore [ 0 ] = 7 0 ; 2. arrays and linked lists

(a) (20%, **) Assume that you have a vector of N integers that are between 1 and M . Write down the pseudo code of an O(N )-time O(M )-space algorithm that determines whether all the integers are different from each other (that is, whether they are all distinct).

1 of 2

(2)

Data Structures and Algorithms (NTU, Spring 2015) instructor: Roger Jang, Hsuan-Tien Lin

(b) (20%, *) Suppose we have a singly linked list with each node defined by the following class:

1 c l a s s node {

2 public :

3 s t r i n g d a t a ;

4 node ∗ n e x t ;

5 }

Write a C/C++ function invert(node *head) that can invert the list pointed to by head and return the pointer to the inverted list without doing any dynamic memory allocation.

(Hint: Note that your function should not need more than 20 lines.) 3. stacks and queues

(a) (20%, **) Show how a stack can be used to evaluate the postfix expression 9 8 7 2 3 * - / - 5 2 - * 3 /

step by step. All the operators within the expression are binary, and you should draw the stack status after each step.

(b) (20%, **) Suppose you have a deque D that stores N elements (d0, d1, . . . , dN −1), in this order, and an initially empty queue Q. Give a pseudo-code description of a function that uses only D and Q (and no other variables or objects) such that the elements within D are reversed—that is, are stored like (dN −1, dN −2, . . . , d0) in D.

4. the evil complexity

The notation f (n) = O(g(n)) applies to functions f (n) and g(n) from N to R+∪{0}, and f (n) = O(g(n)) if and only if there exists n0≥ 1 and c > 0 such that ∀n ≥ n0, f (n) ≤ cg(n). Please only use the definition above in your proof.

(a) (20%, **) Prove or disprove the following statement: If f (n) = O(g(n)), then f (n)2

= O

g(n)2 .

(b) (20%, ***) Prove or disprove the following statement:

If |f (n) − g(n)| = O(1), then 2f (n)= O(2g(n)).

5. (40%, ***) Given a sorted (ordered) vector a with N distinct integers where a[0] < a[1] < . . . <

a[N − 1]. The N integers define N + 1 non-overlapping ranges −∞, a[0], a[0], a[1], . . ., a[N − 1], ∞ in R. Given a value x, a common task is to determine the range that x falls in. Write down the pseudo code of a (log N )-time algorithm that determines the range. For simplicity, you can assume that x is different from all elements in a. (Hint: Think about binary search and finding the largest a[n] that is smaller than x)

2 of 2

參考文獻

相關文件

The bit rate which has more than 4 fail packets to transmission will not be sample, so if all bit rate have fail packets more than 4 or all predict time are too long, we can

In addition, some appropriate and widely used joint models of a latent variable and the cluster measurements are proposed to find the most possible occurring value of a latent

030 846693 ex Parts and accessories of machine tools operated by laser or other light or photon beam processes of a kind used solely or principally for the manufacture of

Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of

Two optimization problems (P 1 ) and (P 2 ) are equivalent if the optimal solution of one problem can be transformed to that of the other so that solving one the problems also gives

Let the coefficient of x 1 be reduced to 1 and use the primal feasibility, dual feasibility and complementary slackness to verify the optimal solution does not change.. (b) What if

Each cabinet requires 250 hours of labor (this is 6 weeks of full time work) and uses 50 board feet of lumber.. Suppose that the company can sell a cabinet for $200, would it

 whole-day face-to-face classes for individual class level(s) - (i) all teachers and school staff (including teaching and non-teaching staff) directly employed by the school have