Which JavaScript coding challenges are good for intermediate level candidates?
For intermediate-level candidates, JavaScript coding challenges should build on core fundamentals while introducing more complex problem-solving techniques, such as manipulating data structures, optimizing algorithms, and working with asynchronous JavaScript. Here are some coding challenges that are well-suited for intermediate-level JavaScript developers, covering various key concepts and problem-solving patterns.
1. Two Sum Problem
- Challenge: Given an array of numbers and a target number, find two numbers in the array that sum to the target.
- Concepts Practiced: Hash tables, time complexity optimization, array manipulation.
- Example:
function twoSum(nums, target) { const map = {}; for (let i = 0; i < nums.length; i++) { const complement = target - nums[i]; if (map[complement] !== undefined) return [map[complement], i]; map[nums[i]] = i; } }
2. Merge Intervals
- Challenge: Given an array of intervals, merge overlapping intervals and return the result as an array of non-overlapping intervals.
- Concepts Practiced: Sorting, interval merging, array manipulation.
- Example:
function mergeIntervals(intervals) { intervals.sort((a, b) => a[0] - b[0]); const result = [intervals[0]]; for (let i = 1; i < intervals.length; i++) { const [start, end] = intervals[i]; const [lastStart, lastEnd] = result[result.length - 1]; if (start <= lastEnd) result[result.length - 1][1] = Math.max(lastEnd, end); else result.push([start, end]); } return result; }
3. Longest Substring Without Repeating Characters
- Challenge: Find the length of the longest substring without repeating characters in a given string.
- Concepts Practiced: Sliding window technique, string manipulation, hash maps.
- Example:
function lengthOfLongestSubstring(s) { let maxLength = 0; let start = 0; const seen = new Map(); for (let end = 0; end < s.length; end++) { if (seen.has(s[end])) start = Math.max(start, seen.get(s[end]) + 1); seen.set(s[end], end); maxLength = Math.max(maxLength, end - start + 1); } return maxLength; }
4. Maximum Depth of Binary Tree
- Challenge: Given a binary tree, find its maximum depth.
- Concepts Practiced: Tree traversal, recursion, depth-first search.
- Example:
function maxDepth(root) { if (!root) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); }
5. Find Kth Largest Element in an Array
- Challenge: Given an array and an integer
k
, find the kth largest element in the array. - Concepts Practiced: Sorting, heaps (for optimal solutions), array manipulation.
- Example:
function findKthLargest(nums, k) { return nums.sort((a, b) => b - a)[k - 1]; }
6. Group Anagrams
- Challenge: Given an array of strings, group anagrams together.
- Concepts Practiced: Hashing, string manipulation, arrays.
- Example:
function groupAnagrams(strs) { const map = {}; for (let str of strs) { const sorted = str.split('').sort().join(''); if (!map[sorted]) map[sorted] = []; map[sorted].push(str); } return Object.values(map); }
7. Implement Debounce Function
- Challenge: Implement a debounce function that delays the execution of a function until after a certain wait time has passed.
- Concepts Practiced: Higher-order functions, closures, asynchronous programming.
- Example:
function debounce(func, delay) { let timeout; return function (...args) { clearTimeout(timeout); timeout = setTimeout(() => func.apply(this, args), delay); }; }
8. LRU Cache Implementation
- Challenge: Implement a Least Recently Used (LRU) cache with set and get operations.
- Concepts Practiced: Data structures, hash maps, linked lists (or arrays), design patterns.
- Example:
class LRUCache { constructor(capacity) { this.capacity = capacity; this.cache = new Map(); } get(key) { if (!this.cache.has(key)) return -1; const value = this.cache.get(key); this.cache.delete(key); this.cache.set(key, value); return value; } set(key, value) { if (this.cache.has(key)) this.cache.delete(key); this.cache.set(key, value); if (this.cache.size > this.capacity) this.cache.delete(this.cache.keys().next().value); } }
9. Implement a Throttle Function
- Challenge: Implement a throttle function that limits the number of times a function can be called in a given time window.
- Concepts Practiced: Higher-order functions, closures, time management.
- Example:
function throttle(func, limit) { let inThrottle; return function (...args) { if (!inThrottle) { func.apply(this, args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; }
10. Valid Parentheses
- Challenge: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
- Concepts Practiced: Stack data structure, string parsing.
- Example:
function isValid(s) { const stack = []; const map = { '(': ')', '{': '}', '[': ']' }; for (let char of s) { if (map[char]) stack.push(map[char]); else if (stack.pop() !== char) return false; } return stack.length === 0; }
These intermediate-level challenges cover important JavaScript and algorithm concepts, including hash maps, recursion, the sliding window technique, debouncing/throttling, and working with data structures like stacks and trees. Practicing these challenges helps solidify JavaScript knowledge and prepares you for common technical interview questions.
For structured learning and more practice with similar problems, DesignGurus.io offers courses like Grokking JavaScript Fundamentals, which covers essential JavaScript concepts and introduces coding patterns that are valuable for interview preparation at the intermediate level. This course will help you get hands-on with various coding patterns and reinforce these skills through practice.
GET YOUR FREE
Coding Questions Catalog