5 Javascript coding interview questions - Part 3
5 Javascript coding interview questions - Part 5
Q1:Implement a function to check if two arrays are equal.
Ans:
function arraysEqual(arr1, arr2) {
return JSON.stringify(arr1) === JSON.stringify(arr2);
}
Q2: Implement a function to perform a deep comparison of two objects
Ans:
function deepEqual(obj1, obj2) {
if (obj1 === obj2) return true;
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return false;
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
for (const key of keys1) {
if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) return false;
}
return true;
}
Q3: Write a function that generates all permutations of a string.
Ans:
function permuteString(str) {
if (str.length <= 1) return [str];
const permutations = [];
for (let i = 0; i < str.length; i++) {
const char = str[i];
const rest = str.slice(0, i) + str.slice(i + 1);
const subPermutations = permuteString(rest);
for (const perm of subPermutations) {
permutations.push(char + perm);
}
}
return permutations;
}
// Example usage:
permuteString('test')
Output:
['test', 'tets', 'tset', 'tste', 'ttes', 'ttse', 'etst', 'etts', 'estt', 'estt', 'etts', 'etst', 'stet', 'stte', 'sett', 'sett', 'stte', 'stet', 'ttes', 'ttse', 'tets', 'test', 'tste', 'tset']
Q4: Write a function to find the maximum sum of a subarray in an array of integers .
Ans:
function maxSubarraySum(arr) {
if (arr.length === 0) {
return 0; // Handle empty array
}
let maxSum = arr[0];
let currentMax = arr[0];
for (let i = 1; i < arr.length; i++) {
currentMax = Math.max(arr[i], currentMax + arr[i]);
maxSum = Math.max(maxSum, currentMax);
}
return maxSum;
}
// Example usage:
const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
const maxSum = maxSubarraySum(arr);
console.log("Maximum sum of a subarray:", maxSum); // Output: 6
Q5: Implement a function to find the longest substring without repeating characters.
Ans:
function longestSubstringWithoutRepeatingChars(str) {
let longestSubstr = '';
let currentSubstr = '';
const charIndexMap = {};
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (charIndexMap[char] === undefined || charIndexMap[char] < i - currentSubstr.length) {
// If the character is not in the current substring
currentSubstr += char;
} else {
// If the character is in the current substring, update the current substring
currentSubstr = str.slice(charIndexMap[char] + 1, i + 1);
}
charIndexMap[char] = i;
if (currentSubstr.length > longestSubstr.length) {
longestSubstr = currentSubstr;
}
}
return longestSubstr;
}
// Example usage:
const inputString = "abcabcbb";
const longestSubstr = longestSubstringWithoutRepeatingChars(inputString);
console.log("Longest substring without repeating characters:", longestSubstr); // Output: "abc"
Explanation:
We maintain a currentSubstr variable to store the current substring being examined and a longestSubstr variable to store the longest substring found so far.
We use a charIndexMap object to keep track of the most recent index at which each character was seen.
We iterate through the input string, checking each character.
If the character is not in the current substring (not in charIndexMap or its last occurrence is not within the current substring), we append it to currentSubstr.
If the character is already in the current substring, we update currentSubstr by slicing it from the index after the last occurrence of that character to the current position.
We update the index of the character in charIndexMap.
If the length of currentSubstr becomes greater than the length of longestSubstr, we update longestSubstr.
After the loop, longestSubstr will contain the longest substring without repeating characters.
Top comments (0)