JsPrQs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

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 = arr.length - 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
console.log(findPairWithSum([1, 2, 3, 4, 5, 6], 9)); // Output: [3, 6]
console.log(findPairWithSum([1, 2, 3, 4], 10)); // Output: null

2. Check if an Array is a Palindrome


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

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

3. Merge Two Sorted Arrays


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

while (i < arr1.length && j < arr2.length) {


if (arr1[i] < arr2[j]) {
result.push(arr1[i++]);
} else {
result.push(arr2[j++]);
}
}

// Add remaining elements


return result.concat(arr1.slice(i)).concat(arr2.slice(j));
}

// Test
console.log(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 < arr.length; right++) {


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

return arr;
}

// Test
console.log(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 = arr.length - 1;
let closest = [arr[left], arr[right]];
let closestDiff = Math.abs(arr[left] + arr[right] - target);

while (left < right) {


const sum = arr[left] + arr[right];
const diff = Math.abs(sum - target);

if (diff < closestDiff) {


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

// Test
console.log(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 = arr.length - 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
console.log(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 = [];
arr.sort((a, b) => a - b);

for (let i = 0; i < arr.length - 2; i++) {


let left = i + 1, right = arr.length - 1;

while (left < right) {


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

if (sum === target) {


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

return triplets;
}

// Test
console.log(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 = heights.length - 1, max = 0;

while (left < right) {


const area = Math.min(heights[left], heights[right]) * (right - left);
max = Math.max(max, area);

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


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

return max;
}

// Test
console.log(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 < arr.length; right++) {


sum += arr[right];

while (sum >= target) {


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

return minLength === Infinity ? 0 : minLength;


}
// Test
console.log(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 < arr.length; i++) {


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

// Remove smaller elements from the end


while (deque.length && arr[deque[deque.length - 1]] < arr[i]) {
deque.pop();
}

deque.push(i);

// Record the max for the current window


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

return result;
}

// Test
console.log(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