Study Union Review Jacob Cornett 1 Computer Science 1 Study Union Practice Problems!This may not be a comprehensive review of everything that will be on your exam and we may go over more during the session. Be sure to check your specific professor s study guides as there are a lot of professors teaching this course this semester and I cannot guarantee that I will cover the material that is on every exam. I apologize for any errors in the guide, I ll try to address those if there are any during the session. Memory Management: What are the arguments to malloc? calloc? realloc? What do they do? What are the differences between these functions? Use the following struct definition for the following question: typedef struct node{ int data; struct node *next; } What would be the code to create an array of type node*? What about a two-dimensional array of type node*?
Study Union Review Jacob Cornett 2 Runtime Analysis: Know the runtime/space complexity of any of the algorithms or data structures that you ve covered in your class including but not limited to: bubble, insertion, selection, merge, quick, radix and bucket sort, binary search tree and AVL tree insertion/deletion, hash table insertion/deletion, heap insertion/deletion, linked list insertion/deletion w/ or w/o a tail pointer, binary search, stack/queue operations, trie insertion/deletion. Empirical Runtime: Find the amount of time that it would take to run an O(n 2 ) algorithm with 10,000 elements if when the same algorithm is run on 1,000 elements the function takes 500ns to run. Evaluate the following recurrence relation using iterative substitution: T (n)=t ( n 2 )+ n 3 +1 Evaluate the following summations: n 2 i i=1
Study Union Review Jacob Cornett 3 n 1 5n 2 i=0 Binary Search: What is the runtime of binary search? What are some applications of binary search? Know how to code binary search up. Recursion: Write a recursive solution to find the nth Fibonacci number: int nfib(int n){ }
Study Union Review Jacob Cornett 4 Write a recursive solution that finds whether a string is a palindrome returning 1 if it is or 0 if it is not: int ispalindrome(char *string){ } Bonus: Write a recursive solution that finds the number of pairs of integers in an array that add up to a value greater than a target number. That is given an array {3, 5, 7, 9} and a target 10 the following pairs add up to a number greater than or equal to 10: {(3,7), (5,7), (3,9), (5,9), (7,9)} (Try this without the position parameter too Hint: use pointer arithmetic) int numberofpairs(int *awesomenumbers, int position){ }
Study Union Review Jacob Cornett 5 Try and find the recurrence relations of the previous functions. Use these to find the run-times of the previous functions. Linked Lists: Write a function that takes the head to a linked list and inserts an element at the head of the list, at the end of the list. Write one that takes the head and tail of a linked list and inserts at the end of the list. Why do we keep a tail pointer?
Study Union Review Jacob Cornett 6 Bonus: Write a function that sorts a linked list and returns the new head. (Use any method you wish to sort the linked list). node *sortll(node *head){ } Stacks: Use a stack to convert the following postfix notation to infix: 5 3 + 4 2 3 * - 9 5 - * /
Study Union Review Jacob Cornett 7 Use a stack to convert the following infix notation to postfix ((9/3) + ((2-1) * 4)) / 7 What is the runtime for the push operation? What about pop? Queues: What are some situations where we would use a queue? What is the runtime of the enqueue and dequeue function if we keep a head and tail pointer?
Study Union Review Jacob Cornett 8 Trace through a breadth first search of the following BST using a queue Show the operations that happen at each step as well as the status of the queue after each enqueue/dequeue operation. 59 / \ 25 72 / \ / \ 9 36 62 84 Sorting: Know how to code up bubble, selection and insertion sort. Know how to trace through bucket, radix, merge and quick sort. Know the logic behind these sorts you may not have to code them up but you should be able to know how they work. Know the runtime and space complexity of these sorts Yes, some of them are not O(n) space complexity. Attempt to trace through the following sorting algorithms using this set of integers: 15, 9, 27, 4, 19, 20, 18, 50, 46, 79 Bubble:
Study Union Review Jacob Cornett 9 Insertion: Selection: Merge: Quick: Bucket:
Study Union Review Jacob Cornett 10 Sort the following set of integers using radix sort: 536, 249, 542, 539, 638, 248, 492, 875
Study Union Review Jacob Cornett 11 This page intentionally left blank for some crazy cool tricks that you may want to know.
Study Union Review Jacob Cornett 12 Binary Search Trees, AVL Trees, Heaps: What is a tree? What is a binary tree? What is a binary search tree? When do we say a BST is self balancing? What is the average height of a BST? What is the best/worst case insert runtime of a BST? Why? What about an AVL tree? When do we say a tree is full? Complete? Perform the following operations on a BST that is not self-balancing: Give the preorder, inorder and postorder traversals of the final tree Insert(26) Insert(30) Insert(15) Insert(22) Delete(15) Insert(36) Insert(20) Insert(23) Insert(14) Delete(22)
Study Union Review Jacob Cornett 13 Perform the following operations on a BST that is self balancing (an AVL tree): Give the preorder, inorder and postorder traversals of the final tree Insert(46) Insert(30) Insert(52) Insert(36) Insert(40) Insert(58) Insert(44) Insert(32) Insert(41) Insert(22) Insert(34) Delete(36) Delete(58) Delete(52)
Study Union Review Jacob Cornett 14 Tries: What does the trienode struct look like? Insert the following words into a trie: Muffin Mulligan Mufasa Cupcake Chess Cheap Coffee Bulwark Bullion Bastion What is the best/worst/average case runtime for insertion into a trie? What about deletion from a trie?what is the runtime to print every word in the trie?
Study Union Review Jacob Cornett 15 Hash Tables: What do we like the length of our hash table to be? When should we expand a hash table? What were the three types of probing we saw for hash tables? What are the benefits and drawbacks of these probing techniques? How did we fix some of the problems with some of those probing techniques? If we have an efficient hash table, what is the average case lookup in the hash table? Base Conversion: Convert 24 10 to base 2 Convert 01001001 2 to base 10 Convert 89AC 16 to base 2
Study Union Review Jacob Cornett 16 Bitwise Operators: What are the three main binary(two operands) bitwise operators? What is the main unary (one operand) bitwise operator? Use those three main binary operators on the following two 2s complement binary numbers: 0011 0110 0101 1101 0110 1011 Now negate the second binary number (Hint: there s something tricky going on here) What are the two types of shifts (not directions)? Do a shift left 2 and a shift right 2 with both of these on the second binary number from before. Be familiar with shift notation ( 01001 << 3 means shift 01001 left 3 bits and 01001 >> 3 means shift right 3 bits).
Study Union Review Jacob Cornett 17 Backtracking: What are some situations where we use backtracking? Do you think you could trace through a maze using backtracking? Could you trace a small nqueens problem using backtracking? What is the basic format for a backtracking algorithm? Are they usually recursive or iterative? What kind of search would we say a backtracking algorithm follows? What is the other kind of search? My list of excellent test-taking tips for the final and the foundation exam: CS1 is a very difficult class for a lot of people, it s a large ramp up in difficulty from Intro to C and it can seem difficult to mash all of that knowledge into your head. Knowing this though, try not to memorize as much as possible. Knowing concepts will be much more beneficial to you both now and in the long run. When taking your exam try not to focus too much on that really difficult problem that seems impossible to solve in the time that you re allotted initially. Read that problem, think on it a bit then move on to problems that you know you can do. Often times you ll find that when you return to that difficult question after a bit you ll have new insight. Obviously if you know you can solve it then go ahead and solve it but don t be afraid to leave the monstrous questions for a bit. Also, don t freak out, I guarantee freaking out will do more harm than good. Those absolutely hideously monstrous problems are meant to be difficult and require thought. Start by going through the data structures and algorithms you ve learned that can be used to solve the problem and then begin thinking about how to implement them. Think about code you ve seen before that s similar to the solution you re looking for. Good luck and may your programs always compile on the first attempt.