Assessment Question

You might also like

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

Assessment question

With answer

1]Write a function to sort the 2-D list


based on column index keeping the rows
intact. This function should take two
arguments - 2D list created above and
column Index and return sorted2D list.
Example:
```
[
[21, 4, 79],
[6, 34, 43],
[15, 54, 23],
]
```
For the above 2D list, if sorted based on
column Index - 2, below should be the
result.
```
[
[15, 54, 23],
[6, 34, 43],
[21, 4, 79],
]

Ans:

const givenInp = [
[21, 4, 79],
[6, 34, 43],
[15, 54, 23]
];

const sortByIndex = (arr, index) => {


return arr.sort((a, b) => a[index] - b[index]);
};
----------------------------------------------------------------------------------------------------------------
----------

2]
You are given an array of integers
representing the heights of walls. Each
integer in the array represents the
height of a wall, and it is assumed that
there is no width associated with the
walls. The distance between two
consecutive walls is equal to 1 unit.

Your task is to find the maximum area


that can be covered by selecting any two
walls. The area calculation is based on
the product of the minimum height of the
two walls and the distance (index
difference) between them.

Wall Heights: [9, 1, 3, 5, 6, 12, 8, 7,


10]
Anaser:
Maximum Area: 72

function findMaxArea(height) {
let maxArea = 0;
let leftIndex = 0;

for (let i = 0; i < height.length; i++) {


for (let j = i + 1; j < height.length; j++) {
let area = (j - i) * Math.min(height[i], height[j]);
if (area > maxArea) {
maxArea = area;
leftIndex = i;
}
}
}

return maxArea;
}

const wallHeights = [9, 1, 3, 5, 2, 145, 8, 7, 10];


const maxArea = findMaxArea(wallHeights);
console.log(maxArea);

-----------------------------------------------------------------------

3]
Find the sum of the array.
Find the number of calls made in the
calls made
{
const arr = [[1, 2, 3], [0, 1, 1], [3,
45, 7], [1, 1, 1], [4, 6, 7], [3, 45, 7],
[3, 45, 7], [1, 1,
1]]; const output =
sum : [6,2,55,3,17,55,55,3]
numberOfCalls: 8 [as no. of small arrays
are 8]

const arr = [
[1, 2, 3],
[0, 1, 1],
[3, 45, 7],
[1, 1, 1],
[4, 6, 7],
[3, 45, 7],
[3, 45, 7],
[1, 1, 1]
];
const findSum = (arr) => {
const sum = arr.map((value) =>
Array.isArray(value) ? value.reduce((acc, val) => acc + val, 0) : value
);
const output = {
sum,
numberOfCalls: arr.length
};
return output;
};

findSum(arr);

4]
Write a function to find a pair of number
from a given array whose sum equals to a
given target.
Example (2, 9, 3, 12, 17, 1) target 10
//Make it as optimized as you can.

Answer:
function findPairWithSum(nums, target) {
let map = new Map();

for (let i = 0; i < nums.length; i++) {


let complement = target - nums[i];
if (map.has(complement)) {
return [complement, nums[i]];
}
map.set(nums[i], i);
}

return null;
}

const nums = [2, 9, 3, 12, 17, 1];


const target = 10;
const pair = findPairWithSum(nums, target);
5]
Write a JavaScript function that takes
the people array as input and returns a
new object where people are grouped by
their 'city'. The object should have city
names as keys, and the corresponding
value for each key should be an array
containing the people who live in that
city.
const people = [
{ id: 1, name: 'Alice', age: 28, city:
'New York' },
{ id: 2, name: 'Bob', age: 22, city:
'London' },
{ id: 3, name: 'Charlie', age: 28,
city: 'New York' },
{ id: 4, name: 'Dave', age: 40, city:
'Tokyo' }
];

Answer:

function groupPeopleByCity(people) {
return people.reduce((acc, person) => {
const { city, ...rest } = person;
acc[city] = acc[city] ? [...acc[city], rest] : [rest];
return acc;
}, {});
}

const people = [
{ id: 1, name: 'Alice', age: 28, city: 'New York' },
{ id: 2, name: 'Bob', age: 22, city: 'London' },
{ id: 3, name: 'Charlie', age: 28, city: 'New York' },
{ id: 4, name: 'Dave', age: 40, city: 'Tokyo' }
];

const groupedPeople = groupPeopleByCity(people);


console.log(groupedPeople);

_________________________________________

6]
// Input
const portfolio =[
{name: 'Mark',stock: 'FB'},
{name: 'Steve',stock: 'AAPL'},
{name: 'Tim',stock: 'AAPL'},
{name: 'Steve',stock: 'MSFT'},
{name: 'Bill',stock: 'MSFT'},
{name: 'Bill',stock: 'AAPL'},
];
// Output
const shareholder =[
{stock:
'AAPL',name:['Steve','Bill','Tim'],count:
3},
{stock:'MSFT',name:['Steve','Bill'],count
:2},
{stock: 'FB',name:['Mark'],count:1},
];

Answer:

function groupPeopleByCity(people) {
const groupedPeople = {};
for (const person of people) {
const { city, name } = person;
if (groupedPeople[city]) {
groupedPeople[city].name.push(name);
groupedPeople[city].count++;
} else {
groupedPeople[city] = { stock: city, name: [name], count: 1 };
}
}
return Object.values(groupedPeople);
}

const people = [
{ id: 1, name: 'Alice', age: 28, city: 'New York' },
{ id: 2, name: 'Bob', age: 22, city: 'London' },
{ id: 3, name: 'Charlie', age: 28, city: 'New York' },
{ id: 4, name: 'Dave', age: 40, city: 'Tokyo' }
];

const groupedPeople = groupPeopleByCity(people);


console.log(groupedPeople);

_________________________________________

7]
You are given a string containing
alphanumeric characters and round
brackets. Your task is to remove the
minimum number of brackets to make the
string valid. A string is considered
valid if either it is empty or if all
brackets are properly closed.

Remove the least number of brackets to


ensure the string is valid. If there are
multiple valid solutions with the same
minimum number of removed brackets,
return any of them.

Examples:
Input: "ja)swi(nd)er"
Output: "jaswi (nd)er"

Input: "j)(("
Output: "j"

In the first example, removing the bracket at index 2 makes


the string valid. In the second xample, removing both brackets
at indices 1 and 2 results in
function removeInvalidParentheses(s) {
const stack = [];
const invalid = new Set();
for (let i = 0; i < s.length; i++) {
if (s[i] === '(') {
stack.push(i);
} else if (s[i] === ')') {
if (stack.length === 0) {
invalid.add(i);
} else {
stack.pop();
}
}
}
while (stack.length > 0) {
invalid.add(stack.pop());
}
let result = '';
for (let i = 0; i < s.length; i++) {
if (!invalid.has(i)) {
result += s[i];
}
}
return result;
}

console.log(removeInvalidParentheses('j(a)swi(nd)er)'));
console.log(removeInvalidParentheses('j)(()'));

You might also like