click below
click below
Normal Size Small Size show me how
JavaScript topics
JavaScript topics review
| Question | Answer |
|---|---|
| What is a class in JavaScript | a blueprint or template for creating objects --`class Dog { constructor(name) { this.name = name } }` |
| How do you create an object from a class | `const dog = new Dog('Buddy')` |
| What is a constructor | Special method in class for setup — `constructor(name) { this.name = name }` |
| What is inheritance in JS | `class Dog extends Animal {}` allows Dog to reuse Animal's properties |
| What is a prototype | Objects inherit methods from prototypes — `obj.__proto__ === Object.prototype` |
| What is closure | A function that remembers its outer scope — `const count = () => { let x = 0 |
| What is hoisting | `var` is hoisted, `let` and `const` are not — accessible before declaration but undefined |
| What is lexical scope | Inner functions access outer variables — `function outer() { let x = 1 |
| What is a stack | LIFO data structure — `stack.push(1) |
| What is a queue | FIFO data structure — `queue.push(1) |
| What is a linked list | List of nodes — `node = { val: 1, next: null }` |
| What is a hash table | Key-value structure — `const map = { name: 'Mikayla' }` |
| What is recursion | Function that calls itself — `function f(n) { return n <= 1 ? 1 : n * f(n-1) }` |
| What is time complexity | Describes performance — `O(1)`, `O(n)`, `O(log n)` |
| What is space complexity | Memory usage — `O(1)` means constant memory |
| What is a pure function | Same input returns same output, no side effects — `x => x * 2` |
| What is currying | function that accepts multiple arguments `add(2)(3)` style functions — `const add = a => b => a + b` |
| What is memoization | Store computed values — `if (memo[n]) return memo[n]` |
| What is a promise | object representing the eventual result Async result — `new Promise((res) => res('done'))` |
| What is async/await | Sugar for promises — `const data = await fetch(url)` |
| What is the event loop | Runs async tasks after call stack is empty |
| What is a higher-order function | Takes or returns function — `arr.map(x => x * 2)` |
| What is a binary tree | Each node has max 2 children — `{ val: 1, left, right }` |
| What is DFS | Use stack or recursion to explore deep before wide |
| What is BFS | Use queue to explore level by level |
| What is binary search | Search sorted array — `O(log n)` — adjust low and high pointers |
| What is debounce in JS | Delay a function until after input stops — `setTimeout(() => callFn(), 300)` |
| What is throttle in JS | Run function at most once per interval — use `setTimeout` inside condition |
| What is a Set in JS | Stores unique values — `new Set([1,2,2])` |
| What is a Map in JS | Key-value pairs — `const m = new Map() |
| What is garbage collection | Automatic memory cleanup for unreferenced data |
| What is the call stack | JS uses call stack to track function calls — LIFO |
| What is the heap | Memory for dynamic data like objects and arrays |
| What is TypeScript | JS + static types — `let x: number = 3` |
| What’s the difference between null and undefined | `null` is intentional empty, `undefined` is not assigned |
| What is OOP IAEP | Programming with classes and objects — `class Car { drive() {} }` |
| What is encapsulation | Hide internal details — use private fields like `#speed` |
| What is inheritance | Reuse behavior — `class Admin extends User {}` |
| What is polymorphism | Different classes, same method name — `admin.login()` vs `user.login()` |
| What is abstraction | Expose only essentials — `User.login()` hides logic |
| Two Sum | Use hash map to track seen values, return indices if complement exists |
| Reverse Linked List | Iterate with 3 pointers: prev, curr, next — set `curr.next = prev` |
| Valid Parentheses | Use stack, push on `(` and pop on `)` — return false if mismatch |
| Best Time to Buy and Sell Stock | Track min price and max profit while iterating |
| Longest Substring Without Repeats | Use sliding window and Set, track max length |
| Merge Intervals | Sort by start time, merge overlapping intervals |
| Climb Stairs | Use dp: `dp[n] = dp[n-1] + dp[n-2]` |
| Invert Binary Tree | Swap left and right children recursively |
| Valid Anagram | Sort both strings and compare or use char count map |
| Group Anagrams | Map sorted word to list — `map[sorted].push(original)` |
| Top K Frequent Elements | Count with hash map, sort or heap for top K |
| Course Schedule | DFS with visited states or topological sort |
| LRU Cache | Use Map + doubly linked list to track order and keys |
| Trapping Rain Water | Use two pointers, track leftMax and rightMax |
| Design URL Shortener | Hash long URL, store in DB, redirect with key, cache frequent ones |
| Design Twitter Feed | Use max-heap for tweets, follow graph, store timestamps |
| Design Rate Limiter | Track request count in time window, use Redis or in-memory store |
| Explain CAP Theorem | Pick 2 of Consistency, Availability, Partition Tolerance |
| What is a load balancer | Distributes traffic across servers — Nginx, AWS ELB |
| What is a CDN | Caches static assets close to user — Cloudflare, Akamai |
| What is sharding | Split DB horizontally by user ID, region, etc. |
| What is vertical vs horizontal scaling | Vertical = more power, horizontal = more machines |
| What is eventual consistency | Data syncs over time — used in distributed systems |