Data Structures Exam 1 Review Quesitons
What is an algorithm? List five characteristics of an algorithm.
- Answer: An algorithm is a set of step-by-step operations to perform a specific task. The five characteristics of an algorithm are precision, uniqueness, finiteness, input, and output.
What is asymptotic notation, and how does it help in analyzing the performance of an algorithm?
- Answer: Asymptotic notation is a mathematical tool used to describe the running time of an algorithm in terms of input size. Asymptotic analysis helps in evaluating the performance of an algorithm in terms of input size and its increase.
What are generic collections in Java?
- Answer: Generic collections hold objects of any data type you can specify the type of data that a collection can hold when you create an instance of the collection.
What is space complexity, and how does it help determine the performance of an algorithm?
- Answer: Space complexity is the amount of memory or space taken by an algorithm to run.
Define time complexity and how it helps determine the performance of an algorithm.
- Answer: Time complexity is the amount of time taken by an algorithm to run.
What are some features the array structure can do
1. copying 2. insertion 3. deletion 4. search & sort
What are some different ways to copy arrays
1. using a loop structure 2. using System.arraycopy() 3. using .copyOf 4. Arrays.asList() 5. .clone()
What is the difference between a single-linked list and a doubly-linked list?
A single-linked list has only one reference and only goes one direction the doubly-linked list that has two references. A single-linked list has nodes that only point to the next node in the list, while a doubly-linked list has nodes that also point to the previous node in the list
How can a stack be used to check if a string is a palindrome?
A stack can be used to check if a string is a palindrome by pushing each character of the string onto the stack, and then popping them off in reverse order. If the characters are the same both when pushed and popped, then the string is a palindrome.
What is an ArrayList in Java, and how does it differ from an array?
An ArrayList is a resizable array that can grow or shrink as needed. Unlike an array, the size of an ArrayList can be changed dynamically during runtime, which makes it more flexible to work with.
Can an interface contain data fields? If not, why?
An interface cannot contain data fields because it is not a class and cannot be instantiated.
What types of values can arrays hold?
Arrays can hold primitives and reference types.
What notation have we learned that gives an upper bound on algorithms performance?
Big O notation
How do you delete an element from an array in Java?
Deletion in an array involves shifting elements to the left. If an element is being deleted at index i,
Assuming circular array implementation of queue, with capacity 4 elements. What is the final content of the array (not the queue) after the following statements: add(A); add(B); remove(); add(C); add(D); remove(); add(E); add(F);
E F C D
T/F? Data elements are always shifted while performing insertion in an ArrayList.
False
T/F? The index of the 'front' of a queue is always less than the index of the 'rear' of the queue for a circular array implementation of a queue?
False
T/F? when implementing data fields and methods in the interface the programmer cannot add: data fields not in the implementation methods not in the implementation constructors
False
What is the storage policy for a queue?
First-In, First-Out(FIFO)
What symbols can be used in arithmetic expressions in addition to parentheses?
In addition to parentheses, arithmetic expressions can also use symbols such as brackets, braces, and angle brackets.
How do you insert an element into an array in Java?
Insertion in an array involves shifting elements to the right. If an element is being inserted at index i
What is the purpose of an interface in Java?
It specifies the methods and actions that an ADT can perform and how it can be used. An interface is like a blueprint or a contract that guarantees how the ADT will function.
Space Complexity
Its the amount of memory or space taken by an algorithm to run
Consider the following code: int calculate(int val, int raise, int total){ int mid = (val + raise)/2; raise = val + mid; total = raise * total; return total; } What is the time complexity of the above?
O(1)
Time complexity of insertion of data in a queue implemented using a circular array?
O(1)
Time complexity of removal of data from a queue implemented using a circular array?
O(1)
What is the time complexity of insertion of data in a queue implemented using a single linked list with reference to both head and tail? (Assume the node after the head is the front of the queue)
O(1)
What is the time complexity of inserting or removing an element at a specific location in a linked list?
O(1) The time complexity of inserting or removing an element at a specific location in a linked list is O(1), assuming we already have a reference to the location.
What is the time complexity of adding and removing elements in a linked list?
O(1) depending if we have referenced the node already The time complexity of adding and removing elements in a linked list is O(1) if the location of the element is known, and O(n) otherwise.
What is the time complexity of inserting an element at the beginning of a single-linked list?
O(1). The time complexity of inserting an element at the beginning of a single-linked list is O(1). We simply create a new node and set its next field to the current head of the list, then set the head of the list to the new node.
Consider the following code: (Assume the array has n elements) for(int i = 1; i < arr.length; i *= 2){ value = value + arr[i]; } What is the time complexity of the above?
O(log n)
What is the time complexity of the Arrays.sort() method in Java?
O(n log n) where n is the number of elements in the array.
Time complexity of insertion of data in a queue implemented using a single linked list with reference to only head? (Assume the node after the head is the front of the queue)
O(n)
What is the time complexity for deleting an element from an ArrayList?
O(n)
What is the time complexity for inserting data into an ArrayList?
O(n)
What is the time complexity to count the number of elements in the linked list?
O(n)
What is the time complexity of an algorithm with a single loop that iterates over an input array of length n?
O(n) The time complexity of an algorithm with a single loop that iterates over an input array of length n is O(n).
What is the time complexity of removing an element from the end of a single-linked list?
O(n) The time complexity of removing an element from the end of a single-linked list is O(n), where n is the number of nodes in the list. We need to traverse the list to find the second-to-last node, which takes O(n) time in the worst case. Then we can set its next field to null to remove the last node.
What is the time complexity of the linearSearch algorithm which performs a linear search through an input array of length n?
O(n) The time complexity of the linearSearchalgorithm is O(n), because in the worst case the algorithm will need to iterate through the entire array, so the number of basic operations is proportional to n.
What is the time complexity of accessing an element in a linked list?
O(n) The time complexity of accessing an element in a linked list is O(n) in the worst case, since we may need to traverse the entire list to find the element.
What is the time complexity of adding and removing elements in an ArrayList?
O(n) The time complexity of adding and removing elements in an ArrayList is O(n), where n is the number of elements in the list.
What is the time complexity of accessing the last element of a single-linked list?
O(n), where n is the number of nodes in the list. We need to traverse the list to find the last node, which takes O(n) time in the worst case.
What is the time complexity of inserting an element at a specified index in a single-linked list?
O(n). The time complexity of inserting an element at a specified index in a single-linked list is O(n), where n is the number of nodes in the list. We need to traverse the list to find the node at the specified index, which takes O(n) time in the worst case. Then we can create a new node and set its next field to the node at the specified index, and update the next field of the previous node to point to the new node.
What is the time complexity of an algorithm with a nested pair of loops that iterate over an input array of length n?
O(n^2). The time complexity of an algorithm with a nested pair of loops that iterate over an input array of length n is O(n^2).
How do you resize an array in Java?
Once an array is created, its length is fixed and cannot be changed. To resize an array, you must allocate the array with a different size and copy the contents of the old array to a new one.
How does the postfix notation make evaluating expressions easier than infix notation?
Postfix notation eliminates the need for parentheses and operator precedence rules by placing operators after their operands.
What is postfix notation?
Postfix notation, is a method of writing arithmetic expressions in which operators come after their operands. For example, "3 4 +" is postfix notation for "3 + 4".
What is prefix notation?
Prefix notation is a method of writing arithmetic expressions in which operators come before their operands. For example, "+ 3 4" is prefix notation for "3 + 4".
How can you copy an array in Java using System.arraycopy()?
System.arraycopy()
Write the equation and Big O notation for: int addUp(int n) { int sum = n * (n+1) /2; return sum; }
T(n) = O(1) O(1) Constant
Write the equation and Big O notation for: int getFirstElement(int[] arr) { return arr[0]; }
T(n) = O(1) O(1) Constant
Write the equation and Big O notation for: int add(int a, int b) { return a + b; }
T(n) = O(1) O(1) Constant
Write the equation and Big O notation for: int binarySearch(int[] arr, int x) { int low = 0; int high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == x) { return mid; } else if (arr[mid] < x) { low = mid + 1; } else { high = mid - 1; } } return -1; }
T(n) = O(log n) + O(1) + O(1) + O(1) + O(1) = O(log n) O(log n) exponential
Write the equation and Big O notation for: boolean contains(int[] arr, int x) { for (int i = 0; i < arr.length; i++) { if (arr[i] == x) { return true; } } return false; }
T(n) = O(n) + O(1) + O(1) = O(n) O(n) Linear
Write the equation and Big O notation for: int sum(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; } return sum; }
T(n) = O(n) + O(1) + O(1) = O(n) O(n) Linear
Write the equation and Big O notation for: int addUp(int n) { int sum = 0; for(int i = 0; i <= n; i++) { sum += 1; } retrun sum; }
T(n) = O(n) + O(n) + O(1) = O(n) O(n) linear
Write the equation and Big O notation for: int max(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; }
T(n) = O(n) + O(n) + O(1) = O(n) O(n) linear
Write the equation and Big O notation for: void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
T(n) = O(n^2) + O(n) + O(1) + O(1) = O(n^2) O(n^2) quadratic
Write the equation and Big O notation for: void printPairs(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { System.out.println(arr[i] + ", " + arr[j]); } } }
T(n) = O(n^2) + O(n) + O(1) = O(n^2) O(n^2) quadratic
What is the difference between the Stack class and the Vector class in Java?
The Stack class is a subclass of the Vector class in Java. While both classes implement a growable array of objects, the Stack class is designed to support the Last-In-First-Out (LIFO) stack data structure, whereas the Vector class supports random access to its elements.
What are the four basic operations of a stack, and what do they do?
The four basic operations of a stack are: empty(): peek(): pop(): push():
What is the head of a linked list and what is its purpose?
The head of a linked list is the first node in the list, and its purpose is to provide a starting point for traversing the list.
What is the Arrays class in Java and what are some of its methods?
The java.util.Arraysclass is a convenience class for various array manipulations, like comparison, searching, printing, sorting, etc. Some of its methods include toString(),sort(), binarySearch(), copyOf(), etc.
What is the tail of a linked list and how is it represented?
The last node of the linked list. Lets the programmer know you have reached the end the last node in the list, and its next field is set to null to indicate the end of the list.
Which Java method removes the first occurrence of a specified element from a single-linked list? A. public void add(E element) B. public boolean remove(E element) C. public void reverse() D. public boolean containsLoop()
The method remove() the first occurrence of a specified element from a single-linked list.
Which Java method reverses a single-linked list in place? A. public void add(E element) B. public boolean remove(E element) C. public void reverse() D. public boolean containsLoop()
The method reverse()
How does the ArrayStack class implement the push() operation?
The push() operation in the ArrayStack class checks if the stack is already full, and if so, reallocates the array to a larger size. It then increments the topOfStack pointer and inserts the new element at the top of the stack.
What are the two common ways to implement a stack, and how can the implementation affect the stack's performance?
The two common ways to implement a stack are using arrays or linked lists.
What are the two parts of a single-linked list node and what do they store?
The two parts of a single-linked list node are a data field to store the element, and a next field to store the reference to the next node in the list.
How can you determine the Big O notation of an algorithm?
To determine the Big O notation of an algorithm, you need to determine its time complexity,
How does a class implement an interface in Java?
To implement an interface in Java, a class must use the "implements" keyword followed by the name of the interface.
T/F? An algorithm in O(n2) is also in O(n3).
True
T/F? Arrays can hold primitives and reference types
True
T/F? A class that implements the interface provides code for the ADT. As long as the implementation satisfies the ADT contract
True
What is the benefit of using an interface in Java?
Using an interface in Java can ensure that different parts of your program can communicate with each other effectively.
How do you handle stack overflow and stack underflow when implementing a stack?
When implementing a stack, you need to consider how to handle stack overflow (adding an element to a full stack) and stack underflow (removing an element from an empty stack).
Can a class implement multiple interfaces in Java?
Yes
What is a list node and what does it contain?
a data field and one or more links to other list nodes
Asymptotic notation
are mathematical tools used to describe the running time of an algorithm in terms of input size
What is the most efficient data structure for storing and accessing a sequence of objects?
array
How can a stack be used to evaluate arithmetic expressions?
by pushing each operand onto the stack, and then performing the corresponding operation when an operator is encountered. The result is then pushed back onto the stack until the expression has been fully evaluated.
Which Java method checks if a single-linked list contains a loop? A. public void add(E element) B. public boolean remove(E element) C. public void reverse() D. public boolean containsLoop()
containsLoop()
How can you compare two arrays in Java?
equals() method of the Arrays class to compare two arrays.
Array is the least efficient data structure for sorting and accessing a sequence of objects
false
T/F? you can copy arrays by assigning one to another int [ ] a = { 9, 5, 4 } int [ ] b = a;
false
insertion and deletion in array involve shifting elements up and down and not left and right depending on whether an element is being inserted
false
Asymptotic analysis
helps in evaluating performance of an algorithm in terms of input size and its increase
ADT (Abstract Data Type)
is a way of organizing and structuring data in a program,
What is a palindrome?
is a word, phrase, or sequence of characters that reads the same backward as forward.
What is the purpose of the isBalanced() method in the balanced parentheses example?
is to determine if a given string of parentheses is balanced or not.
What happens if you add a non-compatible type to a generic collection in Java?
it will generate an error during compile time. However, primitive types will be autoboxed.
Time Complexity
its the amount of time taken by an algorithm to run
What additional methods can some stacks provide, and what do they do?
size(), search(), and toArray().
How can a stack be used to check if symbols in an arithmetic expression are balanced?
stack can be used to check if symbols in an arithmetic expression are balanced by pushing each opening symbol onto the stack and then popping them off when a matching closing symbol is encountered. If the stack is empty at the end of the evaluation, then the expression is balanced.
T/F? To resize an array you must allocate the array with a different size and copy the contents of the old array to a new one
true
T/F? by using an interface, you can ensure that different parts of your program can communicate with each other effectively
true
What does the isBalanced() method do in the balanced parentheses example?
uses a stack to check if an expression has balanced parentheses. It iterates over the characters in the expression, and if it encounters an opening parenthesis, it pushes it onto the stack. If it encounters a closing parenthesis, it checks if the stack is empty.
How do you add elements to an ArrayList in Java?
using the add() method.
Which Java method adds an element to the end of a single-linked list? A. public void add(E element) B. public boolean remove(E element) C. public void reverse() D. public boolean containsLoop()
- Answer: A. The method add() an element to the end of a single-linked list.
What is an Abstract Data Type (ADT)? How is it different from concrete data types?
- Answer: An ADT is a way of organizing and structuring data in a program that is not tied to any specific programming language or implementation.