Frontend Prep Flashcards
What’s the difference between i++ and ++i?
i++ returns the value before it’s incremented
++i returns the value after it’s incremented
let num = 1;
console.log(‘value before incremented: ‘,num++); // 1
let num2 = 1;
console.log(‘value after incremented: ‘,++num2); // 2
What are data structures?
- A way to organize and manage data.
- They’re a bunch of data values put together that have relationships between them that you can manipulate by applying certain functions and manipulations.
What is time complexity?
A measure of how fast an algorithm runs. It’s expressed using Big O notation
What’s space complexity?
A measure of how much auxiliary memory an algorithm takes up. It’s expressed using Big O notation.It’s a measurement of how fast the space taken by a program grows, if the input increases
How will your program save a memory slot?
Your program will save a memory slot or series of memory slots that’s free
What happens if you need to save multiple memory slots?
If your program needs more than one memory block to store into, it would store it in back to back memory slots
How’s memory represented?
0’s and 1’s (binary)
What’s considered constant space complexity?
variable declarations (ex. let i = 0) O(1)
What’s considered linear space complexity (generally)?
When we create new arrays. Where the array will take n slots of space
What’s considered quadratic space complexity (generally)?
when pushing to different elements into an array.
ex. const arr = []; for (let i = 0; i < n; ++i) { for (let j = 0; j < n; ++j) { arr.push(i + j); } }
Here, i and j are two unique elements
What’s considered an O(log n space complexity)?
when you iterate through a series of numbers up to n and increment by multiplying and adding that i value into the array
ex. const arr = []; for (let i = 1; i < n; i *= 2) { arr.push(i); }
Here the array will take (log n) - 1 space
Space complexity: O(log n)
What’s the worst case time complexity of inserting into an array?
O(n), this is b/c when we insert we need to shift n number of elements over to be able to insert at a given index
What’s the worst case time complexity of inserting into a linked list?
O(1), this is b/c we’re just reassigning the pointers to the element before where we want to insert to the new node and assigning the new node’s next to be the following node
Why use bigInt (primitive type)?
BigInt gives us the ability to work with large numbers without having to worry about potentially losing precision (digits) or weird representation issues that hamper accuracy and create bugs.
How does Number and + (unary plus) differ?
They both convert strings to numbers, but + is the fastest way of converting a string to a number b/c it doesn’t perform any operations on the value if it’s ALREADY a number.