0% found this document useful (0 votes)
42 views8 pages

JavaScript Practice Questions Guide

Uploaded by

odeinanyanwu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

JavaScript Practice Questions Guide

Uploaded by

odeinanyanwu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Here's everything redone in JavaScript, covering practice questions,

explanations, and hints for beginner to expert levels.

Beginner Questions
1. Find a Pair with a Specific Sum in a Sorted Array
function findPairWithSum(arr, target) {
let left = 0;
let right = [Link] - 1;

while (left < right) {


const sum = arr[left] + arr[right];
if (sum === target) {
return [arr[left], arr[right]];
} else if (sum < target) {
left++;
} else {
right--;
}
}
return null; // No pair found
}

// Test
[Link](findPairWithSum([1, 2, 3, 4, 5, 6], 9)); // Output: [3, 6]
[Link](findPairWithSum([1, 2, 3, 4], 10)); // Output: null

2. Check if an Array is a Palindrome


function isPalindrome(arr) {
let left = 0;
let right = [Link] - 1;
while (left < right) {
if (arr[left] !== arr[right]) {
return false;
}
left++;
right--;
}
return true;
}

// Test
[Link](isPalindrome([1, 2, 3, 2, 1])); // Output: true
[Link](isPalindrome([1, 2, 3])); // Output: false

3. Merge Two Sorted Arrays


function mergeSortedArrays(arr1, arr2) {
let result = [];
let i = 0, j = 0;

while (i < [Link] && j < [Link]) {


if (arr1[i] < arr2[j]) {
[Link](arr1[i++]);
} else {
[Link](arr2[j++]);
}
}

// Add remaining elements


return [Link]([Link](i)).concat([Link](j));
}

// Test
[Link](mergeSortedArrays([1, 3, 5], [2, 4, 6])); // Output: [1, 2, 3, 4, 5, 6]
Intermediate Questions
4. Move Zeros to the End
function moveZeros(arr) {
let left = 0;

for (let right = 0; right < [Link]; right++) {


if (arr[right] !== 0) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
}
}

return arr;
}

// Test
[Link](moveZeros([0, 1, 0, 3, 12])); // Output: [1, 3, 12, 0, 0]

5. Find the Closest Pair


function findClosestPair(arr, target) {
let left = 0, right = [Link] - 1;
let closest = [arr[left], arr[right]];
let closestDiff = [Link](arr[left] + arr[right] - target);

while (left < right) {


const sum = arr[left] + arr[right];
const diff = [Link](sum - target);

if (diff < closestDiff) {


closest = [arr[left], arr[right]];
closestDiff = diff;
}
if (sum < target) {
left++;
} else {
right--;
}
}
return closest;
}

// Test
[Link](findClosestPair([1, 3, 4, 7, 10], 8)); // Output: [3, 4]

6. Count Pairs with a Specific Sum


function countPairsWithSum(arr, target) {
let left = 0, right = [Link] - 1, count = 0;

while (left < right) {


const sum = arr[left] + arr[right];
if (sum === target) {
count++;
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}

return count;
}

// Test
[Link](countPairsWithSum([1, 2, 3, 4, 5, 6], 7)); // Output: 2 (pairs: [1,6] and
[2,5])
Advanced Questions
7. Find Triplets with a Specific Sum
function findTriplets(arr, target) {
let triplets = [];
[Link]((a, b) => a - b);

for (let i = 0; i < [Link] - 2; i++) {


let left = i + 1, right = [Link] - 1;

while (left < right) {


const sum = arr[i] + arr[left] + arr[right];

if (sum === target) {


[Link]([arr[i], arr[left], arr[right]]);
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
}

return triplets;
}

// Test
[Link](findTriplets([1, 2, 3, 4, 5], 9)); // Output: [[1, 3, 5], [2, 3, 4]]

8. Container with the Most Water


function maxArea(heights) {
let left = 0, right = [Link] - 1, max = 0;

while (left < right) {


const area = [Link](heights[left], heights[right]) * (right - left);
max = [Link](max, area);

if (heights[left] < heights[right]) {


left++;
} else {
right--;
}
}

return max;
}

// Test
[Link](maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])); // Output: 49

9. Minimum Window Size for a Sum


function minWindowSum(arr, target) {
let left = 0, sum = 0, minLength = Infinity;

for (let right = 0; right < [Link]; right++) {


sum += arr[right];

while (sum >= target) {


minLength = [Link](minLength, right - left + 1);
sum -= arr[left++];
}
}

return minLength === Infinity ? 0 : minLength;


}
// Test
[Link](minWindowSum([2, 3, 1, 2, 4, 3], 7)); // Output: 2 (window: [4, 3])

10. Maximum of All Subarrays of Size KK


function maxSubarrayK(arr, k) {
let deque = [];
let result = [];

for (let i = 0; i < [Link]; i++) {


// Remove elements outside the window
if ([Link] && deque[0] === i - k) {
[Link]();
}

// Remove smaller elements from the end


while ([Link] && arr[deque[[Link] - 1]] < arr[i]) {
[Link]();
}

[Link](i);

// Record the max for the current window


if (i >= k - 1) {
[Link](arr[deque[0]]);
}
}

return result;
}

// Test
[Link](maxSubarrayK([1, 3, -1, -3, 5, 3, 6, 7], 3)); // Output: [3, 3, 5, 5, 6, 7]
Let me know if you need further clarification on any specific question or topic! 😊

You might also like