Skip to content

Commit ec16a02

Browse files
authored
Update collections-concepts.md
1 parent 9e1dfea commit ec16a02

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

challenges/collections-concepts.md

+22-59
Original file line numberDiff line numberDiff line change
@@ -2,31 +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 - concepts
6-
7-
1. [Show the different ways of creating an array](#Q1)
8-
1. [Write a program to iterate over an array and print all the values of it](#Q2)
9-
1. [Write a program to append and prepend, single or multiple values in to an array](#Q3)
10-
1. [Show how insertion and removal of elements can happen in the array for given index](#Q4)
11-
1. [Show the different ways of emptying an array which has values](#Q5)
12-
1. [Check if given input is an array or not](#Q6)
13-
1. [Show how an array in JavaScript can act like a stack and queue](#Q7)
14-
1. [Create an array by removing all the holes of the array](#Q8)
15-
1. [Optimize the given statements having lot of logical checks to use a compact and cleaner logic](#Q9)
16-
1. [Write a program to iterate over a 2 dimensional array and print all the values of it](#Q10)
17-
1. [Write a program to store values in to a set](#Q11)
18-
1. [Write a program to store values in to a map](#Q12)
19-
1. [Write a code to iterate over a set](#Q13)
20-
1. [Write a code to iterate over a map](#Q14)
21-
1. [Show how map is different from object to store key value pairs with coding example](#Q15)
22-
1. [Write a program to polyfill `filter` functionality of the Array](#Q16)
23-
1. [Write a program to polyfill `map` functionality of the Array](#Q17)
24-
1. [Write a program to polyfill `reduce` functionality of the Array](#Q18)
25-
26-
---
27-
28-
#### Q1
29-
### Show the different ways of creating an array
5+
<h2 align="center">JavaScript challenges on Collections - concepts</h2>
6+
7+
<br>
8+
9+
### Q. Show the different ways of creating an array
3010

3111
- Arrays are the collection of values in javascript. Array is a special type of object in JavaScript
3212
- Arrays values are indexed from 0 and have special property length which stores the count of elements present in array
@@ -61,8 +41,7 @@ const arr = new Array(1, true, "string");
6141

6242
<br />
6343

64-
#### Q2
65-
### Write a program to iterate over an array and print all the values of it
44+
### Q. Write a program to iterate over an array and print all the values of it
6645

6746
- Arrays can be iterated by using its index to fetch the values
6847
- Arrays also can be iterated with for each style loops
@@ -91,8 +70,7 @@ arr.forEach(val => console.log(val));
9170

9271
<br />
9372

94-
#### Q3
95-
### Write a program to append and prepend, single or multiple values in to an array
73+
### Q. Write a program to append and prepend, single or multiple values in to an array
9674

9775
- Values to the array can be appended using `push` method of array
9876
- Values to the array can be prepended using `unshift` method of array
@@ -128,8 +106,7 @@ To remove the elements from the end of the array `pop` operation can be used but
128106

129107
<br />
130108

131-
#### Q4
132-
### Show insertion and removal of elements can happen in the array for given index
109+
### Q. Show insertion and removal of elements can happen in the array for given index
133110

134111
- Values of the array can be removed from any position using `splice` method of array
135112
- Values of the array can also be inserted to any position using `splice` method of array
@@ -159,8 +136,7 @@ console.log(arr); // [1, 2, 3, 4, 5]
159136

160137
<br />
161138

162-
#### Q5
163-
### Show the different ways of emptying an array which has values
139+
### Q. Show the different ways of emptying an array which has values
164140

165141
- Array can be emptied by giving a new reference of an empty array
166142
- Setting the `length` of the array to 0 will automatically makes the array empty
@@ -192,8 +168,7 @@ arr.splice(0, arr.length)
192168

193169
<br />
194170

195-
#### Q6
196-
### Check if given input is an array or not
171+
### Q. Check if given input is an array or not
197172

198173
- `Array.isArray` is a method which checks if the given argument is an array or not
199174
- Alternatively the `toString` method present on Object prototype can be used to check if it is an array
@@ -214,8 +189,7 @@ Object.prototype.toString.call(arr) === '[object Array]'
214189

215190
<br />
216191

217-
#### Q7
218-
### Show how an array in JavaScript can act like a stack and queue
192+
### Q. Show how an array in JavaScript can act like a stack and queue
219193

220194
- Stack is a 'Last In First Out' data structure can be achieved using `push` and `pop` operations
221195
- Queue is a 'First In First Out' data structure can be achieved using `push` and `shift` operations
@@ -238,8 +212,7 @@ arr.shift();
238212

239213
<br />
240214

241-
#### Q8
242-
### Create an array by removing all the holes of the array
215+
### Q. Create an array by removing all the holes of the array
243216

244217
- Holes are `undefined` value present inside array
245218
- Holes do not get iterated in `filter` which will just fetch all the values except `undefined`
@@ -253,8 +226,7 @@ Holes can be formed when an array value by index is deleted. Example: `delete ar
253226

254227
<br />
255228

256-
#### Q9
257-
### Optimize the given statements having lot of logical checks to use a compact and cleaner logic
229+
### Q. Optimize the given statements having lot of logical checks to use a compact and cleaner logic
258230
```js
259231
// Example1
260232
browser === "chrome" || browser === "firefox" || browser === "IE" || browser === "safari"
@@ -286,8 +258,7 @@ Generally this use case can be implemented for `if` conditions
286258

287259
<br />
288260

289-
#### Q10
290-
### Write a program to iterate over a 2 dimensional array and print all the values of it
261+
### Q. Write a program to iterate over a 2 dimensional array and print all the values of it
291262

292263
- Arrays can be iterated by using its index to fetch the values
293264
- Arrays also can be iterated with for each style loops, with one loop to iterate the rows and inside it for cells
@@ -322,8 +293,7 @@ arr.forEach(rowArr => rowArr.forEach(val => console.log(val)));
322293

323294
<br />
324295

325-
#### Q11
326-
### Write a program to store values in to a set
296+
### Q. Write a program to store values in to a set
327297

328298
- Set lets us store unique values of any type
329299
- Set can be created empty & then added with values or can be initialized also
@@ -349,8 +319,7 @@ set; // 1, 2, 3
349319

350320
<br />
351321

352-
#### Q12
353-
### Write a program to store values in to a map
322+
### Q. Write a program to store values in to a map
354323

355324
- `Map` holds key-value pairs and remembers the original insertion order of the keys
356325
- `Map` can be created empty & then added with values or can be initialized also with key-value pairs
@@ -378,8 +347,7 @@ Unlike objects, `Map` can have any primitive or object as the key
378347

379348
<br />
380349

381-
#### Q13
382-
### Write a code to iterate over a set
350+
### Q. Write a code to iterate over a set
383351

384352
- `set` is an iterable object and can be iterated using for..of loop
385353
- `set` can also be iterated by simple `forEach` loop
@@ -394,8 +362,7 @@ set.forEach(value => console.log(value));
394362

395363
<br />
396364

397-
#### Q14
398-
### Write a code to iterate over a map
365+
### Q. Write a code to iterate over a map
399366

400367
- `map` is an iterable object and can be iterated using for..of loop
401368
- `map` can also be iterated by simple `forEach` loop
@@ -414,8 +381,7 @@ map.forEach((value, key) => console.log(key, value));
414381

415382
<br />
416383

417-
#### Q15
418-
### Show how map is different from object to store key value pairs with coding example
384+
### Q. Show how map is different from object to store key value pairs with coding example
419385

420386
- Map does not contain any keys by default unlike objects which has keys from its prototype
421387
- Map's keys can be any value (including functions, objects, or any primitive) unlike object where keys are only strings
@@ -436,8 +402,7 @@ Maps perform better than objects in most of the scenarios involving addition and
436402

437403
<br />
438404

439-
#### Q16
440-
### Write a program to polyfill `filter` functionality of the Array
405+
### Q. Write a program to polyfill `filter` functionality of the Array
441406

442407
- `filter` iterates over the all values of array and passes value, index and array (itself) as the arguments
443408
- Function returns a new array which filtering the values of the original array
@@ -466,8 +431,7 @@ The solution is a simple polyfill of `filter` and not intended to handle all the
466431

467432
<br />
468433

469-
#### Q17
470-
### Write a program to polyfill `map` functionality of the Array
434+
### Q. Write a program to polyfill `map` functionality of the Array
471435

472436
- `map` iterates over the all values of array and passes value, index and array (itself) as the arguments
473437
- Function returns a new array which is same as the length of the original array
@@ -493,8 +457,7 @@ The solution is a simple polyfill of `map` and not intended to handle all the co
493457

494458
<br />
495459

496-
#### Q18
497-
### Write a program to polyfill `reduce` functionality of the Array
460+
### Q. Write a program to polyfill `reduce` functionality of the Array
498461

499462
- `reduce` iterates over the all values of array and passes value, index and array (itself) as the arguments
500463
- `reduce` accepts an optional initial value which when not provided can be skipped

0 commit comments

Comments
 (0)