Skip to content

Commit e552ff4

Browse files
authored
Update collections-challenges.md
1 parent ec16a02 commit e552ff4

File tree

1 file changed

+27
-74
lines changed

1 file changed

+27
-74
lines changed

challenges/collections-challenges.md

Lines changed: 27 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,11 @@
22
<a href="/README.md#this-is-a-collection-of-modern-interview-code-challenges-on-javascript-suitable-for" id="home">Home</a>
33
</div>
44

5-
## JavaScript interview code challenges on Collections - challenges
6-
7-
1. [Write a function which can concatenate 2 arrays. If only one array is passed it will duplicate it](#Q1)
8-
1. [Write a program to replace 3 center elements of the 1st array by center 3 elements of the 2nd array](#Q2)
9-
1. [Sort the given array of integers in ascending or descending order](#Q3)
10-
1. [Sort the given array of objects in ascending order according the authors lastname](#Q4)
11-
1. [Square all the positive numbers of the array and return the output array](#Q5)
12-
1. [Write a code to generate an array with range of numbers and shuffle them](#Q6)
13-
1. [Check if the user with the name "John" exists in the array of objects](#Q7)
14-
1. [Generate an array of objects with properties id and full name from an array of objects where each object will have id, firstname and lastname](#Q8)
15-
1. [Write a program to calculate the sum of all the values of an array](#Q9)
16-
1. [Get the maximum value from a numbers array along with its index](#Q10)
17-
1. [Find the number of occurences of minimum value in the numbers list](#Q11)
18-
1. [Create an array of length n with all the values of it set to 10](#Q12)
19-
1. [Write the code to remove the duplicates from the array](#Q13)
20-
1. [Design a flat function which flattens an array to any depth](#Q14)
21-
1. [Check if all the students of have passed or not (40 is the pass marks)](#Q15)
22-
1. [Get the average of all the salaries which is greater than 10000 from the department of "IT" from the array of objects)](#Q16)
23-
1. [Extract the list of all the elements from the list of numbers given in 2 arrays](#Q17)
24-
1. [Get the list of all distinct elements which are present in both list of numbers](#Q18)
25-
1. [Extract list of elements present only in the first list given.](#Q19)
26-
1. [Create a function named "average" which can calculate the average of an array and should be available to be called from any Array object.](#Q20)
27-
1. [Write a code to eliminate duplicate objects in an array where each object has an 'id' property which can be used to identify the object and the duplicate object with lower rank to be removed](#Q21)
28-
1. [Create an array which will only accept string values. (Homogeneous array of strings)](#Q22)
29-
1. [Create a Proxy object through which the array can be accessed as usual but also allow to access the values through negative indices](#Q23)
30-
31-
---
32-
33-
#### Q1
34-
### Write a function which can concatenate 2 arrays. If only one array is passed it will duplicate it
5+
<h2 align="center">JavaScript challenges on Collections - challenges</h2>
6+
7+
<br>
8+
9+
### Q. Write a function which can concatenate 2 arrays. If only one array is passed it will duplicate it
3510

3611
- Function can take 2 arguments which concatenates arrays
3712
- 2nd array parameter can be defaulted to 1st array if the value is not passed
@@ -63,8 +38,7 @@ When 2nd argument is not passed, the case is same as duplicating the array
6338

6439
<br />
6540

66-
#### Q2
67-
### Write a program to replace 3 center elements of the 1st array by center 3 elements of the 2nd array
41+
### Q. Write a program to replace 3 center elements of the 1st array by center 3 elements of the 2nd array
6842

6943
- `slice` method on array can be used to fetch the values of range in the array
7044
- `splice` method on array can be used to replace the value of range in the array
@@ -83,8 +57,7 @@ The center most 3 values of array 'a' is replaced by 'b'
8357

8458
<br />
8559

86-
#### Q3
87-
### Sort the given array of integers in ascending or descending order
60+
### Q. Sort the given array of integers in ascending or descending order
8861

8962
- `sort` method sorts the elements of an array in place and returns the sorted array
9063
- It receives a function as an argument, which is used for comparision
@@ -102,8 +75,7 @@ If function is not passed an argument, default sorting will happen
10275

10376
<br />
10477

105-
#### Q4
106-
### Sort the given array of objects in ascending order according the authors lastname
78+
### Q. Sort the given array of objects in ascending order according the authors lastname
10779
```js
10880
// Example
10981
const books = [
@@ -128,8 +100,7 @@ Returning a true or false will not work as the algorithm expects an integer valu
128100

129101
<br />
130102

131-
#### Q5
132-
### Square all the positive numbers of the array and return the output array
103+
### Q. Square all the positive numbers of the array and return the output array
133104

134105
- `filter` is the method on Array which can be used to filter. It receives a function which can return boolean to filter the elements
135106
- `map` is the method on Array which can be used to map the values to new values. It receives a function which can return the modified value
@@ -152,8 +123,7 @@ const squaredPositiveArr = arr.filter((value) => value >= 0).map((value) => valu
152123

153124
<br />
154125

155-
#### Q6
156-
### Write a code to generate an array with range of numbers and shuffle them
126+
### Q. Write a code to generate an array with range of numbers and shuffle them
157127

158128
- An array of numbers in the range can be generated from a function which can take start and end value of the range
159129
- The shuffling can be achieved simply by sorting the array using a function which randomly returns positive or negative numbers
@@ -194,8 +164,7 @@ console.log(shuffledArr) // [5, 4, 7, 10, 3, 6, 8, 2
194164

195165
<br />
196166

197-
#### Q7
198-
### Check if the user with the name "John" exists in the array of objects
167+
### Q. Check if the user with the name "John" exists in the array of objects
199168

200169
```js
201170
const doesJohnExist = arr.some((obj) => obj.name === "John");
@@ -213,8 +182,7 @@ const doesJohnExist = jonhIndex < 0 ? false : true;
213182

214183
<br />
215184

216-
#### Q8
217-
### Generate an array of objects with properties id and full name from an array of objects where each object will have id, firstname and lastname
185+
### Q. Generate an array of objects with properties id and full name from an array of objects where each object will have id, firstname and lastname
218186

219187
- To manipulate array of objects `map` method can be used
220188

@@ -224,8 +192,7 @@ const employeesListWithFullName = arr.map((obj) => { return { id, fullName: obj.
224192

225193
<br />
226194

227-
#### Q9
228-
### Write a program to calculate the sum of all the values of an array
195+
### Q. Write a program to calculate the sum of all the values of an array
229196

230197
- Sum of the values of an array can calculated by iterating and adding all the values of the array
231198
- `reduce` method of array can be used efficiently to calculate the sum with or without initial value
@@ -253,8 +220,7 @@ for(let value of arr){
253220

254221
<br />
255222

256-
#### Q10
257-
### Get the maximum value from a numbers array along with its index
223+
### Q. Get the maximum value from a numbers array along with its index
258224

259225
- `Math.max` is a method which returns maximum value from a given list of values
260226
- `reduce` can also be designed to return the maximum value of each comparision
@@ -290,8 +256,7 @@ Though 2nd solution is verbose compared but has good performance
290256

291257
<br />
292258

293-
#### Q11
294-
### Find the number of occurences of minimum value in the numbers list
259+
### Q. Find the number of occurences of minimum value in the numbers list
295260

296261
- `filter` method can be used to fetch all the minimum values and we can get the count of those valuses
297262

@@ -303,8 +268,7 @@ minArr.length; // count of minimum value occure
303268

304269
<br />
305270

306-
#### Q12
307-
### Create an array of length n with all the values of it set to 10
271+
### Q. Create an array of length n with all the values of it set to 10
308272

309273
- `fill` is a method on Array prototype which fills all the slots of array with the value given passed as the argument
310274

@@ -322,8 +286,7 @@ If an object is passed the object reference is copied to all the slots and not t
322286

323287
<br />
324288

325-
#### Q13
326-
### Write the code to remove the duplicates from the array
289+
### Q. Write the code to remove the duplicates from the array
327290

328291
- Set is a data structure which does not allow duplicate elements
329292

@@ -334,8 +297,7 @@ const distinctArr = [...set];
334297

335298
<br />
336299

337-
#### Q14
338-
### Design a flat function which flattens an array to any depth
300+
### Q. Design a flat function which flattens an array to any depth
339301

340302
- Flat function can be used to flatten the array by recursive call
341303

@@ -356,8 +318,7 @@ function flat(arr){
356318

357319
<br />
358320

359-
#### Q15
360-
### Check if all the students of have passed or not (40 is the pass marks)
321+
### Q. Check if all the students of have passed or not (40 is the pass marks)
361322

362323
- `every` is a method on Array prototype which returns true only if all the elements condition satisfies the condition
363324

@@ -367,8 +328,7 @@ const isAllPass = students.every((student) => student.marks >= 40);
367328

368329
<br />
369330

370-
#### Q16
371-
### Get the average of all the salaries which is greater than 10000 from the department of "IT" from the array of objects)
331+
### Q. Get the average of all the salaries which is greater than 10000 from the department of "IT" from the array of objects)
372332

373333
```js
374334
const itEmployeesWithSalaryGT10K = employees.filter((employee) => employee.salary > 10000 && employee.dept === 'IT );
@@ -378,8 +338,7 @@ const itAvgSalaryGT10K = itTotalSalaryGT10K / itEmployeesWithSalaryGT10K.length;
378338
379339
<br />
380340
381-
#### Q17
382-
### Extract the list of all the elements from the list of numbers given in 2 arrays
341+
### Q. Extract the list of all the elements from the list of numbers given in 2 arrays
383342
384343
- The union array will be the result if all the elements from the 2 arrays are picked
385344
@@ -391,8 +350,7 @@ const distinctArr = [...set1, ...set2];
391350
392351
<br />
393352
394-
#### Q18
395-
### Get the list of all distinct elements which are present in both list of numbers
353+
### Q. Get the list of all distinct elements which are present in both list of numbers
396354
397355
- The intersection array will be the result if the common elements from the 2 arrays are picked
398356
@@ -409,8 +367,7 @@ const distinctIntersectionArr = [...set1].filter(value => set2.has(value));
409367
410368
<br />
411369
412-
#### Q19
413-
### Extract list of elements present only in the first list given.
370+
### Q. Extract list of elements present only in the first list given.
414371
415372
- The only present elements of 1st list will be the result when all the elements of 1st list not present in the 2nd are chosen
416373
@@ -425,8 +382,7 @@ Elements of 2nd list only can be obtained by checking for all the elements of li
425382
426383
<br />
427384
428-
#### Q20
429-
### Create a function named "average" which can calculate the average of an array and should be available to be called from any Array object.
385+
### Q. Create a function named "average" which can calculate the average of an array and should be available to be called from any Array object.
430386
431387
- The function added to Array prototype are accessible to all the objects of Array
432388
@@ -443,8 +399,7 @@ Array.prototype.average = function (){
443399
444400
<br />
445401
446-
#### Q21
447-
### Write a code to eliminate duplicate objects in an array where each object has an 'id' property which can be used to identify the object and the duplicate object with lower rank to be removed
402+
### Q. Write a code to eliminate duplicate objects in an array where each object has an 'id' property which can be used to identify the object and the duplicate object with lower rank to be removed
448403
```js
449404
// Example
450405
const arr = [
@@ -493,8 +448,7 @@ distinctArr = [...map.values()];
493448
494449
<br />
495450
496-
#### Q22
497-
### Create an array which will only accept string values. (Homogeneous array of strings)
451+
### Q. Create an array which will only accept string values. (Homogeneous array of strings)
498452
499453
- Array in JavaScript a collection of values of any type by default
500454
- Proxy can be used to validate and insert to the array only if the value satisfies the condition using `set` trap
@@ -521,8 +475,7 @@ The functionality of the code can be modified to make the array accept any one o
521475
522476
<br />
523477
524-
#### Q23
525-
### Create a Proxy object through which the array can be accessed as usual but also allow to access the values through negative indices
478+
### Q. Create a Proxy object through which the array can be accessed as usual but also allow to access the values through negative indices
526479
```js
527480
// Example
528481
let arr = [10, 20, 30];

0 commit comments

Comments
 (0)