21. Create a function that checks if a given string has balanced parentheses, brackets, and braces.
Required Input:
"{[ ()() ]}"
Expected Output:
true
Code In Javascript
// Check if a string has balanced parentheses, brackets, and braces
function isBalanced(str) {
// Use a stack to track opening characters
// For each closing character, check for matching opening character in stack
}
console.log(isBalanced("{[ ()() ]}"));
Run Code?
Click Run Button to view compiled output
22. Implement a function to find the longest increasing subsequence in an array.
Required Input:
[ 10, 9, 2, 5, 3, 7, 101, 18 ]
Expected Output:
4
Code In Javascript
// Find the longest increasing subsequence in an array
function longestIncreasingSubsequence(arr) {
// Initialize a dp array to store the length of subsequence up to each element
// Update the dp array by comparing each element with previous elements
}
console.log(longestIncreasingSubsequence([ 10, 9, 2, 5, 3, 7, 101, 18 ]));
Run Code?
Click Run Button to view compiled output
23. Write a function that implements the A* search algorithm to find the shortest path in a grid.
Required Input:
grid = [ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ]
start = [ 0, 0 ]
end = [ 2, 3 ]
Expected Output:
The shortest path as an array of coordinates (if exists) or an empty array if no path is possible.
Code In Javascript
// Implement the A* search algorithm for shortest path in a grid
function aStar(grid, start, end) {
// Define heuristic function
// Initialize open and closed sets
// Loop to find shortest path using A* algorithm
}
console.log(aStar([ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ], [ 0, 0 ], [ 2, 3 ]));
Run Code?
Click Run Button to view compiled output
24. Create a function to implement a basic caching mechanism for expensive calculations.
Required Input:
Function to be cached: expensiveCalculation
Cache limit: 5
Expected Output:
100
Code In Javascript
// Implement a caching mechanism with a specified limit
function cacheFunction(func, limit) {
// Initialize a cache object and a queue for tracking entries
// Return a function that checks cache before performing calculation
}
// Example function to cache
function expensiveCalculation(num) {
// Perform an intensive calculation and return the result
}
const cachedExpensiveCalculation = cacheFunction(expensiveCalculation, 5);
console.log(cachedExpensiveCalculation(10));
Run Code?
Click Run Button to view compiled output
25. Write a function that serializes and deserializes a binary tree.
Required Input:
A binary tree to serialize and then deserialize.
Expected Output:
{ val: 1,
left: { val: 2, left: null, right: null },
right:
{ val: 3,
left: { val: 4, left: null, right: null },
right: { val: 5, left: null, right: null } } }
Code In Javascript
// Serialize and deserialize a binary tree
function serialize(root) {
// Convert tree nodes to a string representation
}
function deserialize(data) {
// Convert string back to tree nodes
}
// Example usage
const root = { val: 1, left: { val: 2 }, right: { val: 3, left: { val: 4 }, right: { val: 5 } } };
console.log(deserialize(serialize(root)));
Run Code?
Click Run Button to view compiled output
26. Write a function that converts a number to a string in any base (e.g., base 2, base 16).
Required Input:
num = 255, base = 16
Expected Output:
ff
Code In Javascript
// Convert a number to a string in any specified base
function convertToBase(num, base) {
// Use division and modulo operations to convert to the specified base
}
console.log(convertToBase(255, 16));
Run Code?
Click Run Button to view compiled output
27. Create a function that calculates the Levenshtein distance (edit distance) between two strings.
Required Input:
"kitten", "sitting"
Expected Output:
3
Code In Javascript
// Calculate the Levenshtein distance between two strings
function levenshteinDistance(str1, str2) {
// Initialize a 2D array for dynamic programming
// Update the array based on character insertions, deletions, or replacements
}
console.log(levenshteinDistance("kitten", "sitting"));
Run Code?
Click Run Button to view compiled output
28. Write a function that merges two deeply nested objects, combining properties without overwriting them.
Required Input:
{ a: { b: 1, c: 2 } }, { a: { d: 3 } }
Expected Output:
{ a: { b: 1, c: 2, d: 3 } }
Code In Javascript
// Merge two deeply nested objects without overwriting properties
function deepMerge(obj1, obj2) {
// Recursively merge each property of the objects
}
console.log(deepMerge({ a: { b: 1, c: 2 } }, { a: { d: 3 } }));
Run Code?
Click Run Button to view compiled output
29. Implement a function that simulates a simple finite state machine.
Required Input:
Define states and transitions for a light switch (on/off).
Expected Output:
off
on
Code In Javascript
// Simulate a finite state machine
function createStateMachine(initialState, transitions) {
// Initialize state and define methods to transition states
}
const lightSwitch = createStateMachine("off", {
off: { toggle: "on" },
on: { toggle: "off" }
});
console.log(lightSwitch.currentState); // "off"
lightSwitch.send("toggle");
console.log(lightSwitch.currentState); // "on"
Run Code?
Click Run Button to view compiled output
30. Write a function to solve the knapsack problem, finding the maximum value obtainable within a weight limit.
Required Input:
items = [ { weight: 2, value: 3 }, { weight: 3, value: 4 }, { weight: 4, value: 5 } ]
capacity = 5
Expected Output:
7
Code In Javascript
// Solve the knapsack problem
function knapsack(items, capacity) {
// Initialize a 2D array for dynamic programming
// Update the array based on weight and value of each item
}
console.log(knapsack([ { weight: 2, value: 3 }, { weight: 3, value: 4 }, { weight: 4, value: 5 } ], 5));
Run Code?
Click Run Button to view compiled output