Professional Documents
Culture Documents
Assessment Question
Assessment Question
Assessment Question
With answer
Ans:
const givenInp = [
[21, 4, 79],
[6, 34, 43],
[15, 54, 23]
];
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.
function findMaxArea(height) {
let maxArea = 0;
let leftIndex = 0;
return 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();
return null;
}
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' }
];
_________________________________________
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' }
];
_________________________________________
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.
Examples:
Input: "ja)swi(nd)er"
Output: "jaswi (nd)er"
Input: "j)(("
Output: "j"
console.log(removeInvalidParentheses('j(a)swi(nd)er)'));
console.log(removeInvalidParentheses('j)(()'));