InterviewCake Flashcards

1
Q

What is the greedy approach

A

An algorithm that builds up a solution by choosing the option that looks the best at every step.

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

Simple greedy example

A

Say you’re a cashier and need to give someone 67 cents (US) using as few coins as possible. How would you do it?

Whenever picking which coin to use, you’d take the highest-value coin you could. A quarter, another quarter, then a dime, a nickel, and finally two pennies. That’s a greedy algorithm, because you’re always greedily choosing the coin that covers the biggest portion of the remaining amount.

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

Other greedy examples

A

Looking for a minimum spanning tree in a graph? At each step, greedily pick the cheapest edge that reaches a new vertex.
Trying to fit as many overlapping meetings as possible in a conference room? At each step, schedule the meeting that ends earliest.

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

Binary Search

A

Start in the middle: is it bigger or smaller than the target. Repeat until the target is found. Only works with an ordered list

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

Triangular series

A

A series of numbers where each number could be the row of an equilateral triangle. Always starts with 1 and increases by 1 with each number. Pairs of numbers on each side will always add up to the same value (1 mores than the series’s n.)

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

Total Sum of triangular series

A

(n^2 + n)/2 1+2+3 = 6 (9 + 3)/2 = 6

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

Memoization

A

ensures that a function doesn’t run for the same inputs more than once by keeping a record of the results for the given inputs (usually in an object).

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

Bottom up

A

a bottom-up algorithm “starts from the beginning,” while a recursive algorithm often “starts from the end and works backwards.”

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

Filter

A

var words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly