Google Interview Flashcards
Why is inserting into arrays linear time complexity
Adding to the end of arrays is simple, but at the beginning, which is the worse-case scenario requires reassigning memory address to each piece of data.
Is searching an array linear or constant time
Linear. Requires starting at index = 0 and checking each index for that value
Is deletion linear or constant
Linear. Need to actually find value to delete, in the case index is not known, and removing that value and adjusting the other indexes in response.
What differentiates sets from arrays
Sets don’t allow duplicate values. Inserts are slower since a search has to be performed prior. Search, shift, and insert.
What are some of the advantages/disadvantages of ordered arrays
Search is more optimized, but slower insert times. Always have to conduct a search for inserts so less efficient.
What is a limitation to using binary search
Can only be performed on sorted arrays.
What are the advantages to binary search
Scales well. Number of steps to sort are greatly reduced.
What are some of the steps in performing a binary search
Always choose middle index and check value. Adjust bounds once you’ve established target is less than or greater than middle
What are we quantifying with BigO notation
Quantifying time and space complexity of algorithms for the worst case scenario.
What is the time complexity of bubble sort
Quadratic. Requires nested loops.
What are the major steps behind bubble sort.
Sort each element by comparing one element to the next.
Whats a strategy for finding duplicates in arrays using linear space and linear time?
Use a hash set to record numbers previously seen. Can also use a hash map to record count of a value, but that will require 2N time complexity.
What is the strategy behind selection sort
For each index, record the lowest value that was found in the array. Still produces quadratic time.
What is the strategy behind insertion sort
In best case, sorting algorithm that is linear but quadratic in worst case. Compare each additional element with the sorted sub list.
Review the answer here*
Suppose the first k elements are ordered from least to greatest, even though these elements are not necessarily in their final positions with respect to the entire array.
Move the (k+1)th element left (via repeated swaps) until the first (k+1) elements are ordered from least to greatest.
Repeat until done.
Strategy behind counting sort
Create a count array and have it correspond to the count of that element to its respective index position. So if 2 appears twice in an unsorted array then the count array will have a number 2 and the 2nd index position. This adds quite a bit of space complexity (2N).
What are the advantages of hash tables
In average case scenarios, it’s a constant time for all the major operations. The hashed key values corresponds to a specific memory cell location to store the value data.
What are stacks good for
Great for handling temporary data. Good for memory and linters. Constant time for all operations besides searching
What are some constraints for stacks
Data can only be inserted at the end (top). Data is only read from the top. And data can only be read from the top. (Last in first out)
What are queues used for
Good for asynchronous requests. Processes, printers.
Constraints for queues
Data is only added to the tail and removed/read from the head (FIFO).
Steps for quick sort
Done using recursion! First need to partition array. Set a pivot in a proper place. Treat arrays to left and right of pivot points as their own arrays. Recursively repeat the first two steps. When there is only a subarray with one element left, that is when the base case is reached.
n log(n) time complexity
How are linked lists stored in memory
Unlike arrays, they are not stored in contiguous cells, but spread out. So computer doesn’t need to find large amounts of contiguous cells. Each node references another node at a memory location.
How do linked lists perform for the major functions
Linear for reads and searches. Constant for inserts at beginnings unlike arrays, but linear for adding to the end. Same goes for delete.
What are linked lists best used for
If you’re deleting a lot of data it is superior.
What are the advantages to using a doubly linked lists
Contains an extra pointer, so don’t have to traverse the entire list when deleting to find the previous node, can just look at the previous pointer.
Advantages of a binary tree
Maintains order, has fast lookups deletions and insertions (log complexity). Good for hierarchal relationships (file directories).