Algorithms Flashcards

1
Q

What key algorithms does this course explore?

A

Search, Sort, String Manipulation

These algorithms up in software engineering interviews all the time!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What distinguishes an average programmer from an outstanding software engineer?

A

Understanding how search, sort and string manipulation algorithms work and evolved distinguishes an average programmer from an outstanding software engineer!

Why?

Libraries and frameworks helps us sort data without knowing how sorting or searching algorithms work!

So…

That’s why sorting algorithms come up in interviews all the time!

Action:

How they work, Time & Space complexity, implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What algorithms run in good Big-O?

A

algorithms that run in green or yellow!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the seven essential sorting algorithms all software engineers must know?

A
  1. Bubble Sort
  2. Selection Sort
  3. Insertion Sort
  4. Merge Sort
  5. Quick Sort
  6. Counting Sort
  7. Bucket Sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What must we be able to do with sorting algorithms?

A

Explain how each algorithm works

Know each algorithm’s time and space complexities

Implement each algorithm in given language of choice (Java)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is bubble sort?

A

sort in increasing order (ascending order)

scan array from left to right, if out of order, swap

if right is smaller than left, swap them

requires multiple passes (iterations)

next largest number moves to correct position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Big-O for bubble sort?

A

Best case = already sorted

worst case = reverse sorted

O(n) * O(n) = quadratic (worst case)

O(n) = linear (best case)

passes = each pass bubble one number into place

comparisions = bubbling into place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we implement bubbleSort( ) in Java?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we optimize our implementation of bubblesort( )?

A

reduce O(n) iterations if sorted!

compare only unsorted items by not comparing last items (bubbled up items)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is selection sort?

A

ascending order (smallest to largest)

Multiple passes over an array

In each pass we select the minimum value (from unsorted part) and swap this index with next index(in sorted part).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the time complexity of the selection sort algorithm?

A

O(n) * O (n) : Quadratic

Fairly slow algorithm!

iterate array = O(n) operation!

iterate unsorted array = O(n) operation!

next min value - iterate unsorted part = O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we implement selectionSort ( ) using java?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is insertion sort?

A

each iteration pick one value from unsorted part, insert into sorted part (shift, make space)

Insert how?

We copy the values in the sorted part to the right to make space, then insert the value into the correct position.

Current (unsorted min value) is compared to all values in sorted part.

greater values are shifted to make space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the key difference about insertion sort vs. bubble sort and selection sort?

A

instead of swapping items, we shift items in our array to the right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Big-O for insertion sort?

A

O (n) * O (n) - quadratic

(worst case)
Same as bubbleSort( )!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we implement insertionSort( ) in java?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is merge sort?

A

n (log n) time complexity!

Why?

It’s a type of algorithm called “divide and conquer algorithm.”

How?

it works by breaking the problem down into recursively smaller problems that are easy to solve.

Then it merges those solutions to get the final result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How does merge sort work?

A

dividing and conquering!

break array at middleIndex

divide again at middleIndex (left subarray)

divide again at middleIndex (right subarray)

keep breaking down the array into smaller subparts.

Until, cannot divide anymore

Then compare subparts and merge (sorted)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the Big-O of the merge sort algorithm?

A

Time complexity:

O(n log n) operation!

O (log n) - dividing into subparts

O(n) - read each item, merge subparts

Faster than quadratic!

Space complexity:

O (n) space!

Don’t need to re-create arrays for merge, reuse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do we implement MergeSort algorithm in Java?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do we implement a base condition to stop recursion in merge sort?

A

if their is one item in the array only!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is partitioning?

A

How we move items around a pivot

How?

select last item as pivot

rearrange other elements around it

using two pointers

i = iterate over array (loop variable)

b = boundary (end of left partition)

if the item is smaller than the pivot

increase boundary (left partition), we swap lower value into left partition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the quickSort algorithm?

A

One of the most used sorting algorithms

built into many frameworks and languages

Why?

sorts an array in place without additional space.

O (log n) average time complexity!

How?

first, select a pivot

re-order the items around this pivot (partitioning)

items on right are larger than pivot

items on left are smaller than pivot

Pivot is now in place (doesn’t move)

select next item as pivot

partition again

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the Big-O of the quick sort algorithm?

A

Why?

O(n) - have to iterate all items and swap (partitioning)

# of times can partition - depends on pivot selection

w/ the right pivot selection on average can run in

O(log n) time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do we implement the quickSort algorithm?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are comparision based sorting algorithms?

A

They sort data based on comparision

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are non comparision sorting algorithms?

A

They use basic math to sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is count sort?

A

non comparision sorting algorithm

we count occurrences and use that to sort data

How?

allocates a separate counts array

iterates input array, updating counts array

iterates counts array, sorting input array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

When should we use count sort?

A

values must be positive to use as index in counts array

if input range has gaps, will end up with many 0s in counts array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the Big - O of counting sort algorithm?

A

O(n) time complexity!

faster than O(n log n)

Why?

Space - O(k) where k = max range of input array

Populate counts array - iterate input array O(n) operation!

iterate counts array, refil input array = O(n + k)

Cost?

time-memory tradeoff!

allocating extra memory!

31
Q

How do we implement the counting sort algorithm?

A

O (n) time complexity

O(k) space complexity!

32
Q

What is the bucket sort algorithm?

A

iterate input array, dividing items into buckets

iterate buckets, sort each bucket using sorting algorithm

merge buckets

can sort buckets in parallel

each item in the bucket is a linked-list

33
Q

What is Big-O of bucket sort algorithm?

A

Range: O(n) to O(n^2)

single bucket (worst case) - O(n^2) using insertionSort on buckets

many buckets each a single item (best case) - O(n) time complexity but at a memory cost

34
Q

How can we refactor bucketSort to make it cleaner?

A
35
Q

What are the five essential searching algorithms all software engineers must know?

A

These come up in coding interviews!

Understand how it works!

Understand its Big-O!

There are more searching algorithms but these five come up in coding algorithms!

Five algorithms:

  1. Linear Search - O (n)
  2. Binary Search - O (n)
  3. Ternary Search - O (n)
  4. Jump Search - O (n)
  5. Exponential Search - O (n)
36
Q

What is Linear Search?

A

Simplest search algorithm.

relatively slow O(n) operation!

works well with a small list!

How it works:

Iterate over a list

inspect items, if value matches search, return it

otherwise return -1

37
Q

What is the time complexity of linear search?

A

O(1) best case - item is first in list

O(n) worst case - item is last in list

38
Q

How do we implement linear search in Java?

A
39
Q

What is binary search?

A

Divide and conquer algorithm

O (log n) operation!

In each step, we cut the number of items to inspect by half!

First, calculate middle index

Next, item is either on left or right side of partition

Repeat, calculate middle index

Stop, index = value

40
Q

What type of lists does binary search only work on?

A

sorted lists!

if our list is unsorted, we either must sort it first or use linear search!

Why?

Divide and conquer algorithm!

When?

if we have multiple items to lookup, pays off to sort then use binary search!

41
Q

What is Big-O for binary search?

A

O(log n) time - divide and conquer algorithm

O(log n) space - recursive calls on stack

O (1) space - iteration implementation

42
Q

How do we implement binary search using recursion?

A
43
Q

How do we implement binary search using iteration?

A
44
Q

What is ternary search?

A

divide list into three parts (partitions) recursively

partitionSize = (right - left) / 3

midPointOne = left + partitionSize

midPointTwo = right - partitionSize

compare these index values to our search value

if not a midPoint, could be in partition, recursive part

eventually, the method will return the index of search value!

45
Q

What does the logarithm function tell us?

A

To which power to raise a number to get another number

Ex.

Log base 2 of 8

tells us what power to raise two to get eight!

To which power should we raise 2 to get 8?

Answer 3.

46
Q

What is the Big-O of ternary search?

A

As we divide our list into more parts, search algorithm gets slower!

Binary search is faster than ternary search!

Why?

Worst case scenario a list of 10 items has 10 partitions, O(n) have to compare value to every item in list

47
Q

How do we implement ternary search using recursion?

A
48
Q

What is jump search?

A

Divide list into blocks (blockSize = squrt(n))

jump to that block where value may exist

How?

Compare last item in block with target value

if value is greater, jump to next block

When?

value is less than last item in block

do linear search in that block

49
Q

What is the Big-O of jump search?

A

O( squareroot(n) )

An improvement to linear search!

Not as fast as binary search!

50
Q

How do we implement jumpSearch in Java?

A
51
Q

How do we refactor this code in jumpSearch?

A

Add the conditional statement to the while loop

52
Q

What is exponential search?

A

Start w/ small range

check if target is w/in range

if it’s not, double the range.

check if target is in this range.

if target is potentially w/in range

do a binary search w/in range (after upper bound of previous range)

53
Q

What is Big-O of exponential search?

A

O ( log i )

i = # items in range

54
Q

How do you implement exponentialSearch in Java?

A
55
Q

What are the string manipulation algorithms?

A
  1. Find the number of vowels in a string
  2. Reverse a string
  3. Reverse the order of words in a sentence
  4. Check if a string is a rotation of another string
  5. Remove duplicate characters in a string
  6. Find the most repeated character in a string
  7. Capitalize the first letter of each word in a sentence
  8. Detect if two strings are anagrams (contains exact same letters in any order)
  9. Check if a string is a palindrome. (If we read palindrome string left to right get same letters).
56
Q

What are some useful Java classes/methods for solving string manipulation excercises?

A
57
Q

How do we implement countVowels ( )?

A
58
Q

How do we implement reverse ( ) to reverse a string?

A
59
Q

How can we reduce the time complexity of reverse ( ) from O(n^2) to O(n)?

A

Use the java class StringBuilder to create mutable strings

eliminates O(n) for creating a new string everytime we add a letter!

60
Q

What’s another implementation of reverseWords ( ) using built in Java functions?

A
61
Q

How do we implement reverseWords ( )?

A
62
Q

How do we implement areRotations ( )?

A

If this string is one million characters the space complexity is very large therefore we need to implement this method using a for loop to compare values!

63
Q

What is a different way of implementing areRotations ( )?

A
64
Q

How do we implement removeDuplicates ( )?

A
65
Q

How do we implement getMaxOccuringChar ( ) w/o using a HashMap <> ( )?

A

Can use an array

Store each character at ASCII value as index

increment value at ASCII index

66
Q

How do we implement maxOccurringChar ( )?

A
67
Q

How do we implement capitalize ( )?

A

Popular Interview Question!

Edge cases:

whitespace front / back - . trim

whitespace between words - .replaceAll w/ single space

pass empty space - .isEmpty, return empty string

68
Q

How do we implement isAnagram ( ) using sorting?

A

Very popular Interview Question!

ask if solution must be case insensitive

Edge cases:

pass empty string

pass white space

pass null - check

69
Q

What does adding optimizations do to the readability and maintainability of our code?

A

It makes it less readable and less maintainable

don’t do too much pre optimizations (early stage optimizations)

if affects code readability and maintainability

Take context in count

Ask questions about execution, size of inputs, etc.

Ask about size of input

70
Q

SM How do we implement isPalindrome ( )?

A
71
Q

What is a code monkey?

A

Writes code without understanding the problem

Writes code without understanding what they’re doing

72
Q

What is a software engineer?

A

A software engineer is one who developers properly engineered solutions to today’s problems, not tomorrows problems that may never happen!

Be a software engineer, not a code monkey who doesn’t really understand the problem / what solution is doing

73
Q

What is mosh’s advice?

A

Study other resources as well!

Especially to pass interviews at Google, Microsoft and Amazon!

Check out:

leetcode.com

interviewcake.com

online communities for popular interview questions!