CS177 Spring 2012 © Popescu Midterm Examination 1 Part 1 1. The binary (i.e. base 2) representation of the hexadecimal (i.e. base 16) number AB16 is: a. 10112 b. 1010 10112 c. 1100 11012 d. 10102 e. None of the above Answer: b Explanation: A16 in binary is 10102, B16 in binary is 10112, hence AB16 in binary is 1010 10112 2. A 2 minute song is stored in a 2MB file. Can the song be streamed over a 128kbps network? a. Yes b. No Answer: b Explanation: 2MB = 2*1024 KB = 2*1024*8 Kb. 128Kb is transferred in 1 sec Therefore, 2*1024*8 Kb will be transferred in (2*1024*8)/128 = 128 sec > 2min 3. A computer uses 32b addresses. What is the maximum amount of memory that the computer can address? a. 32b b. 32Kb c. 32Gb d. 32GB e. None of the above Answer: c Explanation: 2^32 bytes = 4*2^30B = 4GB = 32Gb 1 4. You are given a digital image stored in a 2-D array A and two pairs of indices (i0, j0) and (i1, j1). The two pixels corresponding to the index pairs are immediate neighbors if the following condition is true? Recall that “!=” means “not equal”. a. (i0-i1) < 2 and (j0-j1) < 2 b. (i0-i1) < 2 and (j0-j1) < 2 and (i0-i1) > -2 and (j0-j1) > -2 c. (i0-i1) < 2 and (j0-j1) < 2 and (i0-i1) > -2 and (j0-j1) > -2 and (i0 != i1 or j0 != j1) d. All of the above e. None of the above Answer: c Explanation: (i0-i1) < 2 and (j0-j1) < 2 and (i0-i1) > -2 and (j0-j1) > -2 is true for the immediate neighbors (which means that (i0-i1) and (j0-j1) difference should not be greater than 1) (i0 != i1 or j0 != j1) means the two pixels should not be the same, in other words, a pixel is not an immediate neighbor of itself 5. A linked list has nodes that store a value using 1 byte, and a link to the next node using 4 bytes. If the list has 100 nodes, then: a. 100 bytes are used to store values b. 400 bytes are used to store links c. Only 20% of the total memory used goes towards storing values d. a and b e. a and b and c Answer: e Explanation: A single node has 1 byte value and 4 bytes link. So 100 nodes will have 100 bytes value and 400 bytes link. Total storage of 100 nodes = 100 + 400 = 500 bytes. Therefore, percentage memory taken for storing values = (100/500)*100 = 20%. 6. Every internal node in a binary tree has exactly two children. How many nodes are there on level 4, where level 0 corresponds to the root, level 1 corresponds to the children of the root, level 2 corresponds to the children of the children of the root, etc.? a. 16 b. 8 c. 32 d. 64 e. None of the above Answer: a Explanation: No of nodes in level 0 = 2^0 = 1 (the root) No of nodes in level 1 = 2^1 = 2 No of nodes in level 2 = 2^2 = 4 2 No of nodes in level 3 = 2^3 = 8 No of nodes in level 4 = 2^4 = 16 In general, number of nodes in level n = 2^n 7. A graph is encoded using adjacency lists. Given a node A and a node B, how can you determine whether you can reach B from A using exactly one intermediate node? a. You traverse the adjacency list of A to see if it stores B b. You traverse the adjacency list of B to see if it stores A c. You traverse the adjacency list LA of A, and, for every node X in LA you traverse the adjacency list LX of X to see if it stores B d. You look for intersections between the adjacency lists of A and B e. None of the above Answer: c Explanation: Traversing the adjacency list LA of A will give us all the nodes adjacent to A (1 hop from A). For every node X that is adjacent to A, we see the adjacency list Lx of X. We B exists in Lx, then B is adjacent to X (1 hop from X). Therefore, B is two hops from A. 8. Traversing a five dimensional array can be done using five nested for loops. a. Yes, the statement is true b. No, since there are no five dimensional arrays c. The traversal could also be done with while loops d. a and c e. None of the above is true Answer: d Explanation: Since there are 5 dimensions we will have to traverse each dimension using nested loops. Therefore, 5 nested loops are required. The loops used can be “for” loops or “while” loops. 9. Two variables A and B are swapped as follows a. A = C, A = B, B = C b. C = A, B = C, A = B c. C = A, A = B, B = C d. A = B, B = A e. None of the above Answer: c Explanation: Here we are using temporary value C to swap. First store the value of A to C. Then copy the value of B to A (1st part of swapping is done and both B and A have the same value at this point). Then, B needs the value of A to complete the swap. Since we have stored the original value of A in C, we do B=C at the end to store the original value of A to B 3 10. In the following algorithm for inserting an element into a sorted array, what is the purpose of the iins = n instruction? Input: A // sorted array of integers (increasing from left to right) n // number of elements in array (array size) B // integer to be inserted Output: A // sorted array with n+1 elements (original element and B) InsertSorted(A, n, B) // idea is to find where to insert B and then to insert B iins = n for i = 0 to n-1 if B < A[i] then iins = i break endif endfor for i = n down to iins+1 A[i] = A[i-1] endfor A[iins] = B return A, n+1 endInsertSorted a. The instruction is not really needed since iins doesn’t need to be initialized as it is overwritten the first time we take the then branch of the if B < A[i] statement. b. The instruction simply initializes iins just in case the then branch of the if statement is never taken, but iins could be initialized to any value (e.g. 0, 1, etc.), and not necessarily to n. c. The instruction handles the case when the number to be inserted is larger than the last (i.e. largest) element of A. In that case, the then branch of the if is never taken, and the correct insertion point is n, i.e. after the last element. d. b and c e. a and b Answer: c Explanation: We are initializing iins = n. If the number that we are inserting is the largest of the array, then it will be inserted at the very end of the array. Therefore, the if() branch will never become true and will not execute as B<A[i] will not be true as B is the largest element being inserted. Hence the 2nd for loop will not execute as iins will still be n. Therefore, A[iins] = B, will insert B at position n as iins = n 4 11. The running time of the following algorithm is Input: A // array of integers n // number of elements in array (array size) Output: B // array with elements of A, sorted in ascending order SortMin(A, n) // idea is to repeatedly extract minimum from original array for i = 0 to n-1 B[i] = A[i] // copy array A into B Endfor for i = 0 to n-2 imin = MinIndex(B, i, n) Swap(B[i], B[imin]) endfor return B endSortMin a. Linear since the second for loop iterates n-1 times, which is approximated to n b. Linear since the first and the second for loops are not nested, and both iterate approximately n times c. Linear since the bodies of the two for loops contain a constant number of steps (i.e. 1 and 2 steps, respectively) d. Quadratic since MinIndex finds the index of the smallest element in linear time and it is executed once for every iteration of the second for loop. e. b and c Answer: d Explanation: The 2nd loop takes n iterations and every iteration calls MinIndex which also has a loop that takes n iterations. Therefore, the complexity becomes n*n = n^2. (Think of it as nested loops) 12. Which of the following statement(s) is true about the 2-D convolution algorithm. a. It contains 4 nested for loops because it involves a 4-D array b. The outer two for loops iterate over the 2-D image c. The inner two for loops iterate over the region of overlap between the kernel and the 2D image d. If the image size is w x h, the kernel size is k x k, and no border pixels are ignored, the running time is w x h x k x k e. b, c, and d 5 Answer: e Explanation: Please look at the 2-D convolution algorithm. No 4-D arrays are involved. Only two 2-D arrays exist, one being the input image and the other being the kernel. The running time is w*h*k*k, because there are (w*h) pixels in the input image and for each pixel in the input image, we are overlapping the (k*k) kernel on that pixel. 13. An algorithm designed to insert a node into a sorted list L first finds the links prev and curr to the two consecutive nodes in between which the new node is to be inserted. Each of the two links prev or curr could be NULL. The link to the new node is stored in variable N. How is the new node linked into the list L? a. prev->next = N N->next = curr b. if prev == NULL then L =N else prev->next = N endif N->next = curr c. N->prev = prev N->next = curr d. N->next = curr if prev == NULL then prev->next = N else L=N endif e. None of the above Answer: B Explanation: If N is to be inserted in the first position, the prev=NULL and curr=FirstNode, hence we modify the head pointer L to point to N and modify the link of N to point to curr, otherwise, we make the prev point to N and next of N point to curr. 6 14. What is the sequence of recursive calls for the following recursive algorithm for counting the nodes in a binary tree, for the tree shown? T0 Input: T // link to root of binary tree Output: // count of nodes CountBTR(T) if T == NULL then return 0 endif return CountBTR (T->left) + 1 + CountBTR (T->right) endCountBTR l0 l1 l2 a. CountBTR(T0), CountBTR(I0), CountBTR(NULL), CountBTR(NULL), CountBTR(I1), CountBTR(I2), CountBTR(NULL), CountBTR(NULL), CountBTR(NULL) b. CountBTR(T0), CountBTR(I0), CountBTR(NULL), CountBTR(NULL), CountBTR(I1), CountBTR(I2), CountBTR(NULL), CountBTR(NULL) c. CountBTR(T0), CountBTR(I0), CountBTR(I1), CountBTR(I2) d. CountBTR(T0), CountBTR(I1), CountBTR(NULL), CountBTR(I2), CountBTR(NULL), CountBTR(NULL), CountBTR(I0), CountBTR(NULL), CountBTR(NULL) e. CountBTR(I0), CountBTR(I1), CountBTR(I2) Answer: a 7 15. Given a sorted array A of integers of length n and an integer B, one can find whether B appears in A faster than linear time. a. No, because any algorithm has to look at least at all elements of A once, and that is linear time b. No, because there are no algorithms faster than linear time c. No, because if the algorithm doesn’t check one of the elements of A, it could be that that element is equal to B and therefore the conclusion that B does not appear in A is incorrect d. An algorithm can use the fact that A is sorted to avoid looking at all elements of A e. None of the above Answer: d Explanation: Binary search can search an element in sorted array in log(n) time. This has been covered in project 2 (Fast search) 8 Part 2 Given a one dimensional array A of n integer temperature values, design an algorithm that computes the number of bits needed to encode a temperature value according to the range of values in A. Remember, log2m = a implies that 2a = m. a. What is the output of the algorithm for the following input A = {75, 75, 77, 82, 77, 80, 81, 80, 81, 76, 75, 76, 78, 79}, n = 14 b. Give a step-by-step description of the algorithm. c. Give a pseudocode description of the algorithm. Answer: a) 3 bits (since the max value = 82 and min value = 75, the range = 8 elements, which can be encoded using 3 bits) b) Step by step description: 1. Since we need to find the number of bits to encode temperature values according to the range of values stored in A, we need to figure out a way to find range. 2. Intuitively, the range is the difference between the max and the min value in A 3. Therefore, we first find the max value, and then the min value and subtract min from max to find the range. 4. Number of bits needed to encode range = log2(range) c) Pseudocode: Input: Array A Size of Array n Output: X number of bits required to encode range of values in A 9 NoOfBitsToEncode(A, n) max = min = A[0] //Initialize max value and min value of A to first element of A //find the max value For i = 0 to n If(A[i]>max) max = A[i] endif End for //find the min value For j = 0 to n If(A[j]<min) min = A[i] endif End for Range= max – min Numberofbits = ceiling(log2(Range+1)) Return Numberofbits 10