Linear Data Structures Flashcards
What is Big O notation?
Big O is a mathematical notation that describes the limiting behavior of a function where the argument tends toward a particular value or infinity.
What is Big-O?
Helps us determine if an algorithm is scale-able or not
When do we use Big O notation?
Describe the performance of an algorithm
What are the five common Big-O curves?
O( 1 ) - constant time
O ( n ) - linear time
What are the five common time complexity curves?
How does linear big - O grow?
Linearly and in direct proportion to the size of the input
What is O(n^2)?
Quadratic.
Nested loops
How does the logarithmic curve behave?
It slows down as the input size grows
What is exponential time complexity?
The exponential curve grows faster as the input grows.
What is the shortcut to run JVM in debug?
Control + D
When would we have a quadratic Big-O value?
When we use nested loops that each performs an iteration over the entire set of values.
What are the linear data structures?
- Arrays
- Linked Lists
- Stacks
- Queues
- Hash Tables
What are arrays?
Reference type
fixed-length data structure.
Values are indexed sequentially in memory.
Optimal for looking up values by their index.
What can we use arrays for?
We use arrays to store lists of values like strings, objects or anything.
These items are stored sequentially in memory.
Can store strings, objects, numbers or anything.
What are the pro’s of arrays?
Looking up the index is very fast
why?
item index lookup doesn’t involve loops or complex data access operations.
What are the disadvantages of arrays?
They’re fixed length.
Not good for storing values that need to be added or deleted frequently.
Adding or deleting an item requires copying old array into a new array with a cost of O(n).
How do we keep track of the number of items in our array?
Using a private variable “count”
increment count for each new item
How do we implement resizeIfRequire ( ) in our addItem method of our array class?
Create a new array x 2 size
copy items into new array
O ( n ) operation!
How do we implement the removeAt ( ) method in our array class?
Shift items left
Copy items from [index + 1] into [index]
How can we implement the print ( ) method in our custom array class?
Use a for loop to iterate over each index
+ print on console
How can we implement the max ( ) method in our Array class?
What is run time complexity?
O(n):
have to iterate over the entire array to find the largest number.
This number may be at the end of the array (worst case scenario).
How to implement the insertAt ( ) method in our Array class?
How to implement the reverse ( ) method in our Array class?
How to implement the intersect ( ) method in our Array class?
Foreach loop + nested if loop
What is the Big-O of the addItem ( ) to an array?
It’s O(n)
linear time complexity
Why?
Array’s are static in size.
In the worst case, we have to create a new array, copy all items to this array
What is the Big O of the removeAt ( ) operation in arrays?
O(n) - Linear time complexity.
Why?
Worst case it’s the first item in the array, then we have to shift all items over which is an O(n) operation!
What is the Big O of find by value in an array?
O ( n )
Worst case have to iterate the entire array!
What is the Big-O of lookup by index in an array?
O(1)
why?
What is the syntax for working with Arrays in Java?
Two ways :
Create with an empty array
Insert values, omit the new operator
What is the Array’s class in Java?
Declared in Java.util
Static methods to perform operations on Arrays
What are the two implementations of dynamic arrays in Java?
Vector class:
will grow by 100% of size when full
methods are synchronized (single thread access)
ArrayList class:
will grow by 50% of size when full
What are useful ArrayList methods?
.add(item) - dynamic adds item
.remove(index) - removes item at index
.contains(item) - boolean if item is in list
.size( )- # of items in array
.toArray - converts to regular array object, not arrayList class
AA What are circular arrays?
Very important, powerful concept in data structures
What is covered in the linked-list collection?
What are linked lists?
A data structure consisting of a group of nodes in sequence.
Each node contains one value and one address for the next node.
Unlike array’s linked-lists can grow automatically.
When do we use linked-lists?
In building more complex data structures
they are the second most common after Arrays
What are the key differences between Arrays and LinkedLists?
Dynamic arrays:
ArrayList - Grow by 50-100% of the size, wasting memory
LinkedLists:
Don’t waste memory
require extra memory for storing the next node location
How do you work with a linked-list in java?
java.util.LinkedList package
can access pre-built methods
.insert ( )
.delete ( )
What should we always access when we use java.util LinkedList class?
Integer class (primitive wrapper)
Because we are using the integer class
A wrapper class for the primitive integer
Not, a primitive type
LL If we don’t declare the type in a LinkedList class what can we store automatically?
Any type of object
could be a string
could be an integer
What are the operations on Linked Lists?
AddLast ( )
AddFirst ( )
IndexOf ( )
Contains ( )
RemoveFirst
RemoveLast
Reversing LinkedList
kth-node from end
LL What are the run time complexities of the insert ( ) method on linked - lists?
What is the Big-O time complexity for various operations on linked-lists?
addFirst - O(1)
addLast - O(1)
removeFirst - O(1) - update head to point to next item
removeLast - O(n) - getPrevious method is an O(n) operation
contains - O(n) - traverses entire list to look for item (worst case))
indexOf - O(n) - traverses entire list to look for item (worst case))
toArray - O(n) - traverse entire list of nodes, storing into an array
getPrevious - O(n) - traverse entire list
What is the run-time complexity of the various linked operations?
Lookup
By value = O(n) - because the value we are looking for may be stored in the last node (worst case).
By index = O(n) - the nodes of the linkedlist may be all over in memory, each node has to keep a reference of the next node, in worst case scenario you have to traverse all the nodes to find the index.
Insert
At end = O(1) - Just need to create a new node and have end (tail) point to it.
at beginning = O(1) - just need to create a new node and have start (head) point to it.
middle = O(n) - have to iterate over entire list, incrementing an index variable before inserting new node.
Delete
at end = O(n) because have to interate entire list to find the reference to the last node (tail).
at beginning = O(1)
middle = O(n)
How do we implement the private Node class in our LinkedList class in Java?
How do we implement the addLast method in our LinkedList class in Java?
O(1) operation -
create a new node, update last.next to point to it.
How do we implement the addFirst method?
O(1) operation
Create a node, set node.next to first
How do we implement the removeFirst method in our LinkedList class?
O(1) operation
update the second item as the first item
How do we implement the removeLast ( ) method in our LinkedList class?
O (n) operation
getPrevious ()
iterates the entire list
How do we implement the IsEmpty( ) method in a clean modern way?
Extract into a private method and call that method
How do we implement the indexOf (int item) method in our LinkedList class?
O(n) operation
traverses entire list search for item (worst case)
How do we implement the contains ( ) method in our LinkedList class?
O(n) operation
indexOf ( ) method
Searches entire list worst case
How do we implement the getPrevious ( ) method in our LinkedList class?
O(n) operation
search entire list for node worst case
How do we implement the toArray ( ) method in our LinkedList?
O (n) operation
copy every node in the list into an array
How do we implement the reverse ( ) method in our LinkedList class?
Popular interview question:
Reverse LinkedList in place.
O(n) runtime complexity
Establish two pointer variables
previous (last) and current (first)
Keep track of the last node with a variable next (n)
iterate through changing the direction of links
then set the last and first nodes
How do we implement the getKthFromEnd ( ) in our Linked List class?
O(n) operation
Iterate until the end!
Popular Interview Question:
Find the Kth node from the end of a Linked List in one pass
LL How do we implement printMiddle ( ) in our Linked List class?
Find relationships between pointers
Nodes increases by 2 as middle increases by one
How do we implement hasLoop ( ) method in our Linked List class?
What are circular LinkedLists?
Last node references first node
Doubly and Singly LinkedLists can be circular!
Benefits:
circular navigation!
UI in a form that moves to the first item after completion
Playlists that repeat after the last song
What is a doubly linked-list?
Doubly LinkedList:
O(1) operation for removeLast ( ) !
It has an extra field to reference the next node and the previous node!
Singly linked-lists:
Only have a reference to the next node!
O(n) for delete at end.