- Published on
Javascript DSA Questions With Answers
- Authors
- Name
- Ganesh Negi
Javascript machine coding round questions

1. Write a program in javascript to flatten a array without use of predefined functions ?
Question means: Given an array that can contain nested arrays, flatten it into a single-level array.
function flattenArray(arr) {
let result = [];
for(let i = 0; i < arr.length; i++) {
const temp = arr[i];
if(Array.isArray(temp)) {
const flatItem = flattenArray(temp);
for(let j = 0; j < flatItem.length; j++) {
result.push(flatItem[j]);
}
} else {
result.push(temp);
}
}
return result;
}
Explanation line by line:-
Declares a function named flattenArray that takes one argument: arr, which is expected to be an array (possibly nested).
Creates an empty array result that will eventually hold all the flattened elements.
A for loop that iterates over each item in the input array arr.
Stores the current element from the array into a variable item.
Checks if the current item is itself an array using Array.isArray().
If item is an array, we recursively call flattenArray on it.
This is the key to handling deeply nested arrays.
The result of this recursive call is stored in flatItem, which is itself an array of flattened values.
We loop through each element of the flattened sub-array flatItem.
We push each element into our result array.
This part spreads out the nested result into the main result array without using .concat() or .flat().
If the current item is not an array (i.e., it's a number, string, etc.), we push it directly into the result.
End of the loop and function.
We return the fully flattened array stored in result.
Steps:
Input: [1, [2, [3, 4], 5], 6]
1 → not an array → push to result.
[2, [3, 4], 5] → is an array → recursion.
2 → push
[3, 4] → recursion → push 3, 4
5 → push
6 → push
Result: [1, 2, 3, 4, 5, 6]
2. Your goal is to combine values by name in an array of objects — summing values where the name is the same.
const arr = [
{ name: 'Ganesh', amount: 1000 },
{ name: 'Amit', amount: 2000 },
{ name: 'Ganesh', amount: 2000 },
{ name: 'Amit', amount: 8000 },
];
Goal is
[
{ name: 'Ganesh', amount: 3000 },
{ name: 'Amit', amount: 10000 }
]
Solution:
const arr = [
{ name: 'Ganesh', amount: 1000 },
{ name: 'Amit', amount: 2000 },
{ name: 'Ganesh', amount: 2000 },
{ name: 'Amit', amount: 8000 },
];
const result = [];
const map = {};
// Step 1
for (let i = 0; i < arr.length; i++) {
const { name, amount } = arr[i];
if (map[name]) {
map[name] += amount;
} else {
map[name] = amount;
}
}
// Step 2: Convert map to array of objects
for (const name in map) {
result.push({ name, amount: map[name] });
}
console.log(result);
// [ { name: 'Ganesh', amount: 3000 }, { name: 'Amit', amount: 10000 } ]
3. Write a program in javascript to swap two without using third variable ?
Method 1: Using Arithmetic Operations
a = 10
b = 20
a = a + b # a = 30
b = a - b # b = 10
a = a - b # a = 20
print("a:", a)
print("b:", b)
Method 2: Using Bitwise XOR
a = 10
b = 20
a = a ^ b
b = a ^ b
a = a ^ b
print("a:", a)
print("b:", b)
4. Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array. ?
Input: arr[] = [12, 35, 1, 10, 34, 1]
Output: 34
Explanation: The largest element of the array is 35 and the second largest element is 34.
Solution
function getSecondLargest(arr) {
let n = arr.length;
let largest = -1, secondLargest = -1;
// Finding the largest element
for (let i = 0; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
}
// Finding the second largest element
for (let i = 0; i < n; i++) {
// Update second largest if the current element is greater
// than second largest and not equal to the largest
if (arr[i] > secondLargest && arr[i] !== largest) {
secondLargest = arr[i];
}
}
return secondLargest;
}
let arr = [12, 35, 1, 10, 34, 1];
console.log(getSecondLargest(arr))
5. Given an array of n integers, the task is to find the third largest element. All the elements in the array are distinct integers. ?
function thirdLargest(arr) {
let n = arr.length;
let first = -Infinity, second = -Infinity,
third = -Infinity;
for (let i = 0; i < n; i++) {
// If arr[i] is greater than first,
// set third to second, second to
// first and first to arr[i].
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
// If arr[i] is greater than second,
// set third to second and second
// to arr[i].
else if (arr[i] > second) {
third = second;
second = arr[i];
}
// If arr[i] is greater than third,
// set third to arr[i].
else if (arr[i] > third) {
third = arr[i];
}
}
// Return the third largest element
return third;
}
let arr = [1, 14, 2, 16, 10, 20];
console.log(thirdLargest(arr));
6. Reverse Integer Without Predefined Functions ?
function reverseInteger(num) {
let reversed = 0;
let isNegative = false;
if (num < 0) {
isNegative = true;
num = -num; // make it positive for processing
}
while (num > 0) {
let digit = num % 10; // get the last digit
reversed = reversed * 10 + digit; // add digit to reversed
num = Math.floor(num / 10); // remove last digit
}
return isNegative ? -reversed : reversed;
}
// Example usage:
console.log(reverseInteger(1234)); // Output: 4321
console.log(reverseInteger(-5678)); // Output: -8765