#100devs - Big O (cohort 02)

Class 63 of our Free Web Dev Bootcamp for folx affected by the pandemic. Join live T/Th 6:30pm ET leonnoel.com/twitch and ask questions here: leonnoel.com/discord

Skip to first slide
Slide 1 of 69

Leon Noel

Big O

#100Devs

"But that was the shit that I prayed about I thank God I made it out
Damn it feels good to go pick up my momma and take her out
Lil Tony got paper now all in my bank account, and it ain't shit to debate about"


Slide 2 of 69

Agenda


Slide 3 of 69

Questions

About class or life


Slide 4 of 69

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 69

!newsletter


Slide 6 of 69

#HUNTOBER2022

!templates


Slide 7 of 69

4 More Classes

 Building Hitlists & How To Get A Job


Slide 8 of 69

FRIDAY TEA

6:00pm ET in Classroom


Slide 9 of 69

SUNDAY OFFICE HOURS

1pm ET 

 

 

 

 


Slide 10 of 69

Daily Question

Daily Standup

(Mon-Fri 6pm EST)

 

 

 

 


Slide 11 of 69

WE NEED TO TALK

Ready? Others?


Slide 12 of 69

P.R.E.P

Parameters, Returns, Examples, Pseudo Code


Slide 13 of 69

Methods Questions


Slide 14 of 69

Brute Force


Slide 15 of 69

ITS ALL YOU NEED


Slide 16 of 69

But... Let's Open More Doors


Slide 17 of 69

A data structure is simply an organized way of storing data  


Slide 18 of 69

It is a pattern of collecting and organizing data for performing various operations correctly and efficiently.


Slide 19 of 69

BUT WHY?


Slide 20 of 69

What if I wanted to store information about a stock trader?

 

Object or Array?


Slide 21 of 69

Object Makes The Most Sense

let stockTrader = {
    firstName: 'Bob',
    lastName: 'Wehadababyitsaboy',
    location: 'New York, New York, USA',
    jobTitle: 'Trading Associate',
    yoe: 1,
    recentTrades: ['tsla','gme','amc']
}

Slide 22 of 69

What if I wanted to store recent tsla purchase prices?

 

Object or Array?


Slide 23 of 69

Array Makes The Most Sense

let tslaPurchasePrices = 
	[208.17, 205.05, 217.57, 220.48]

Slide 24 of 69

 An algorithm is just    the steps you take to solve a problem


Slide 25 of 69

Thinking through appropriate

algorithms is how we can efficiently solve problems


Slide 26 of 69

We care about
Space
&

Time


Slide 27 of 69

Space
How much memory is being used


Slide 28 of 69

Time
How many operations are executed per input


Slide 29 of 69

When taking about the efficiency of a solution we can use

Big-O Notation


Slide 30 of 69

BUT WHY?


Slide 31 of 69

Big-O notation mathematically describes the complexity of an algorithm in terms of time and space


Slide 32 of 69

BUT WHY?

All computers, environments, ect... are different


Slide 33 of 69

We care about

rough estimates


Slide 34 of 69

Common Complexities


Slide 35 of 69

Git Connected Big O Article


Slide 36 of 69

O(1)

For all inputs to our algorithm there is and will always be only one operation required


Slide 37 of 69

O(1) Example

No matter how many inputs are located in num there will only ever be one operation needed!

const nums = [1,2,3,4,5] 
const firstNumber = nums[0]

Slide 38 of 69

O(N)

For all inputs to our algorithm there will be one

operation per input


Slide 39 of 69

O(N) Example

Here we sum the numbers in the array. We have to add each number to a running sum, so we have to operate on each one. This means one operation per input.

const nums = [1,2,3,4,5]
let sum = 0
for(let num of nums){
	sum += num
}

Slide 40 of 69

O(1) VS O(N)

Summing function for a sorted, contiguous array of integers that starts with the number 1? Could easily be O(n) but...

const sumContiguousArray = function(ary){ 
  //get the last item
  const lastItem = ary[ary.length - 1]
  //Gauss's trick
  return lastItem * (lastItem + 1) / 2
}
const nums = [1,2,3,4,5]
const sumOfArray = sumContiguousArray(nums)

Slide 41 of 69

O(N^2)

Quadratic

Text

const hasDuplicates = function(num){ 
  for(let i = 0; i < nums.length; i++){
    const thisNum = nums[i]
    for(let j = 0; j < nums.length; j++){
      if(j !== i){ 
        const otherNum = nums[j]
       }
    }  
    if(otherNum === thisNum) return true
  }
  return false
}

const nums = [1,2,3,4,5,5]
hasDuplicates(nums) //true

Slide 42 of 69

O(N^2)

Here we’re iterating over our array, which we already know is O(n), and another iteration inside it, which is another O(n). For every item in our list, we have to loop our list again to calculate what we need.


Slide 43 of 69

O(LOG N)

Logarithmic Time
As you loop your operations are halved

Divide and Conquer?


Slide 44 of 69

Git Connected Big O Article


Slide 45 of 69

Committing these to memory is important


Slide 46 of 69

I have a problem


Slide 47 of 69

The price of boxes is 

TOO DAMN HIGH


Slide 48 of 69

Let's take a look


Slide 49 of 69

I want to create a bidding platform


Slide 50 of 69

How can we find the min and max bid?


Slide 51 of 69

Let's loop through

all the bids

Text

const findMaxBid = function(bids){
  let maxBid = bids[0],
      minBid = bids[0]
  for(let i = 0; i < bids.length; i++){
    for(let j = 0; j < bids.length; j++){
      if(bids[i] > bids[j] && bids[i] > maxBid ){ 
        maxBid = bids[i]
       }else if(bids[i] < bids[j] && bids[i] < minBid){
         minBid = bids[i]
       }
    }  
  }
  return [minBid, maxBid]
}

const allBids = [2,7,3,1,4,5,5]
console.log(findMaxBid(allBids))

Slide 52 of 69

Really bad code?,

but what is the complexity?


Slide 53 of 69

Git Connected Big O Article


Slide 54 of 69

What if one loop found the min and the other found the max


Slide 55 of 69

Finding the max bid

Text

const findMaxBid = function(bids){
  let maxBid = bids[0]
  for(let i = 0; i < bids.length; i++){
    if(bids[i] > maxBid){
      maxBid = bids[i]
    }
  }
  return maxBid
}

const allBids = [1,2,7,3,4,5,5]
console.log(findMaxBid(allBids))

Slide 56 of 69

2n?


Slide 57 of 69

What if our data was already sorted?


Slide 58 of 69

2?


Slide 59 of 69

What is an example of constant time?


Slide 60 of 69

What is an example of linear time?


Slide 61 of 69

What is an example of quadratic time?


Slide 62 of 69

What is an example of logarithmic time?


Slide 63 of 69

Let's explore some JS complexities


Slide 64 of 69

.pop() ?


Slide 65 of 69

.shift() ?


Slide 66 of 69

.length ?


Slide 67 of 69

forEach, map, reduce ?


Slide 68 of 69

Homework

 

Do: Take your first application step!
Do:  https://frontendmasters.com/courses/practical-algorithms


Slide 69 of 69