Javascript Specific Flashcards
What is the time complexity of trim()?
O(n).
What is the time complexity of split()?
O(n).
What is the time complexity of sort()?
O(n log n).
What is the time complexity of map()?
O(n).
How do you get a random number in a range?
Math.random() * (max - min +1) + min.
How do you get the square root?
Math.sqrt().
How do you import a priority queue
import { MinPriorityQueue } from “datastructuresjs/priority-queue”
How do you set custom priority in a priority queue thats been imported from “datastructuresjs/priority-queue”
const carsQueue = new PriorityQueue((a,b) => a.priority - b.priority);
What is modulo propogation?
Modulo propagation is a technique where instead of computing the full value of a large number, we maintain only its remainder when divided by a given number (m). This allows us to efficiently check divisibility or perform modular arithmetic without handling large numbers.
126
1️⃣ Start with remainder 0.
2️⃣ Process “1” → (0 × 10 + 1) % 3 = 1
3️⃣ Process “2” → (1 × 10 + 2) % 3 = (10 + 2) % 3 = 12 % 3 = 0 ✅
4️⃣ Process “6” → (0 × 10 + 6) % 3 = 6 % 3 = 0 ✅
What can you use to calc big numbers in JavaScript
BigInt and 0n terminology
k = huge number
let num1 = 5n // number n is a bigInt
let num2 = BigInt(k)
let div = num2 / num1
How do you find a characters position in alphabet
char.charCodeAt(0) - “a”.charCodeAt(0)
How do you get the first values of a set?
myset.values().next().value … values is an iterator
What makes maps different from objects?
map perserve insertion order … this is really useful for things like lru cache
How do you get the first key of a map?
mymap.keys().next().value …. keys is an iterator
When are generators not useful in DSA
Generators are not ideal for:
Sorting Algorithms (since they require storing elements).
Random Access Data Structures (like heaps, where you need direct index-based access).
Cases Requiring Multiple Passes Over Data (generators are one-time iterators).
When are generators useful in DSA
✔ Generators shine in large data, trees, graphs, and infinite sequences.
✔ Use them for DFS, BFS, lazy evaluation, and streaming computations.
✔ Avoid them for sorting and direct index-based access (heaps, arrays).