Skip to content

Commit d3b9235

Browse files
authored
Update functions-challenges.md
1 parent 77ca41f commit d3b9235

File tree

1 file changed

+21
-56
lines changed

1 file changed

+21
-56
lines changed

challenges/functions-challenges.md

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +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 Functions - challenges
6-
7-
1. [Design a Calulator interface for 2 number inputs which can perform sum, difference, product and dividend whenever invoked on the same interface](#Q1)
8-
1. [Design a private counter function which exposes increment and retrive functionalities](#Q2)
9-
1. [Write a polyfill for bind function](#Q3)
10-
1. [Write a function which will create a function bounded to the context like `bind`, but can be overridden when the context is set explicitly](#Q4)
11-
1. [Write a function which helps to achieve multiply(a)(b) and returns product of a and b](#Q5)
12-
1. [Create a function which takes another function as an argument and makes it eligible for currying or partial application](#Q6)
13-
1. [Design a function which helps to do debouncing](#Q7)
14-
1. [Design a function which helps to do throttling](#Q8)
15-
1. [Design an interface which limits the number of function calls by executing the function once for a given count of calls](#Q9)
16-
1. [Write a singleton function to create an object only once](#Q10)
17-
1. [Design a function with toggle functionality for given list of inputs where toggle function accepts list of values to be toggled upon](#Q11)
18-
1. [Create a range function which returns an array for the provided inputs as start and end](#Q12)
19-
1. [Write a function which takes a function as an argument to achieve memoization](#Q13)
20-
1. [Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c](#Q14)
21-
1. [Design a function which can keep recieving the arguments on each function call and returns the sum when no argument is passed](#Q15)
22-
1. [Create an interface for a function such that whenever a function is triggered the system should log the time. Do not modify the function code](#Q16)
23-
1. [Create an interface exposing subscribe and publish functionality, which allows publishing data which in turn invokes all the subscribers with the data](#Q17)
24-
25-
---
26-
27-
#### Q1
28-
### Design a Calulator interface for 2 number inputs which can perform sum, difference, product and dividend whenever invoked on the same interface
5+
<h2 align="center">JavaScript challenges on Functions - challenges</h2>
6+
7+
<br>
8+
9+
### Q. Design a Calulator interface for 2 number inputs which can perform sum, difference, product and dividend whenever invoked on the same interface
2910
```js
3011
// Example
3112
const calc12And5 = Calculator(12, 5);
@@ -68,8 +49,7 @@ The solution provided is one of the way to achieve the interface. The design and
6849

6950
<br />
7051

71-
#### Q2
72-
### Design a private counter function which exposes increment and retrive functionalities
52+
### Q. Design a private counter function which exposes increment and retrive functionalities
7353

7454
```js
7555
function privateCounter(){
@@ -102,8 +82,7 @@ counter.retrieve(); // 8
10282

10383
<br />
10484

105-
#### Q3
106-
### Write a polyfill for bind function
85+
### Q. Write a polyfill for bind function
10786

10887
- The `bind` method creates a new function that, when called, has its this keyword set to the provided context
10988

@@ -129,8 +108,7 @@ This is a simple polyfill for bind without handling corner cases. It does not wo
129108

130109
<br />
131110

132-
#### Q4
133-
### Write a function which will create a function bounded to the context like `bind`, but can be overridden when the context is set explicitly
111+
### Q. Write a function which will create a function bounded to the context like `bind`, but can be overridden when the context is set explicitly
134112

135113
- The functionality is similar to `bind` with exception that if there is a context set during the execution it will override
136114

@@ -157,8 +135,7 @@ This functionality is also known as 'Soft Binding'
157135

158136
<br />
159137

160-
#### Q5
161-
### Write a function which helps to achieve multiply(a)(b) and returns product of a and b
138+
### Q. Write a function which helps to achieve multiply(a)(b) and returns product of a and b
162139
```js
163140
// Example
164141
multiply(2)(4); // 8
@@ -180,8 +157,7 @@ function multiply(num1){
180157

181158
<br />
182159

183-
#### Q6
184-
### Create a function which takes another function as an argument and makes it eligible for currying or partial application
160+
### Q. Create a function which takes another function as an argument and makes it eligible for currying or partial application
185161

186162
- Function can take another function as argument and accept the arguments on the returned functions till the expected arguments are reached
187163
- The arguments can be 1 or multiple, and the actual function will be called once the count of expected arguments are reached
@@ -213,8 +189,7 @@ sum(1,2)(3,4); // called like partial application
213189

214190
<br />
215191

216-
#### Q7
217-
### Design a function which helps to do debouncing
192+
### Q. Design a function which helps to do debouncing
218193

219194
- The `debounce` function forces a function to wait a certain amount of time before running again
220195
- The function is built to limit the number of function calls to improve the performance
@@ -244,8 +219,7 @@ Context is replaced while debounce function call in presence of a context. If no
244219
245220
<br />
246221
247-
#### Q8
248-
### Design a function which helps to do throttling
222+
### Q. Design a function which helps to do throttling
249223
250224
- The `throttling` function forces a function to run once in an amount of time for one or multiple calls
251225
- The function is built to limit the number of function calls to improve the performance
@@ -279,8 +253,7 @@ Last arguments to the throttled function is saved so that most recent arguments
279253
280254
<br />
281255
282-
#### Q9
283-
### Design an interface which limits the number of function calls by executing the function once for a given count of calls
256+
### Q. Design an interface which limits the number of function calls by executing the function once for a given count of calls
284257
285258
- function forces a function run to for specific number of times in a given number of execution calls
286259
- The function is built to limit the number of times a function is called
@@ -310,8 +283,7 @@ Sampling is different from throttling in the way that sampling limits the execut
310283
311284
<br />
312285
313-
#### Q10
314-
### Write a singleton function to create an object only once
286+
### Q. Write a singleton function to create an object only once
315287
316288
- Singleton is a design pattern which restricts the creation of only one object from a given interface
317289
- When requested multiple times, same object is returned
@@ -348,8 +320,7 @@ Here both 'instance1' and 'instace2' are referencing to the same object
348320
349321
<br />
350322
351-
#### Q11
352-
### Design a function with toggle functionality for given list of inputs where toggle function accepts list of values to be toggled upon
323+
### Q. Design a function with toggle functionality for given list of inputs where toggle function accepts list of values to be toggled upon
353324
```js
354325
// Example
355326
var hello = toggle("hello");
@@ -388,8 +359,7 @@ function toggle(...values){
388359
389360
<br />
390361
391-
#### Q12
392-
### Create a range function which returns an array for the provided inputs as start and end
362+
### Q. Create a range function which returns an array for the provided inputs as start and end
393363
```js
394364
// Example
395365
range(3, 6) // [3, 4, 5, 6]
@@ -421,8 +391,7 @@ function range(start, end) {
421391
422392
<br />
423393
424-
#### Q13
425-
### Write a function which takes a function as an argument to achieve memoization
394+
### Q. Write a function which takes a function as an argument to achieve memoization
426395
427396
- Memoization is an optimization technique used primarily to speed up the programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again
428397
- Function can be designed to use a cache storage (using `map` or `object`) which stores the values of function output against the input
@@ -461,8 +430,7 @@ Stringification of arguments done in order for the function to work for multiple
461430
462431
<br />
463432
464-
#### Q14
465-
### Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c
433+
### Q. Create a single function which can perform sum(a, b, c), sum(a, b)(c), sum(a)(b, c) and sum(a)(b)(c) and returns sum of a, b and c
466434
```js
467435
// Example
468436
sum(2)(4)(6); // 12
@@ -516,8 +484,7 @@ function sum() {
516484
517485
<br />
518486
519-
#### Q15
520-
### Design a function which can keep recieving the arguments on each function call and returns the sum when no argument is passed
487+
### Q. Design a function which can keep recieving the arguments on each function call and returns the sum when no argument is passed
521488
522489
- The function can be designed to return another function which maintains the closure over the previous sum value
523490
- The check for breaking condition can be added using the argument check for `undefined`
@@ -565,8 +532,7 @@ In the code value is checked if it is undefined reason being 0 is a falsy value
565532
566533
<br />
567534
568-
#### Q16
569-
### Create an interface for a function such that whenever a function is triggered the system should log the time. Do not modify the function code
535+
### Q. Create an interface for a function such that whenever a function is triggered the system should log the time. Do not modify the function code
570536
571537
- Function call can be handled using Proxy in JavaScript
572538
- `apply` keyword in proxy can be used to achieve the functionality without modifying the existing function code
@@ -596,8 +562,7 @@ This technique is helpful in logging or managing the data being passed to & retu
596562
597563
<br />
598564
599-
#### Q17
600-
### Create an interface exposing subscribe and publish functionality, which allows publishing data which in turn invokes all the subscribers with the data
565+
### Q. Create an interface exposing subscribe and publish functionality, which allows publishing data which in turn invokes all the subscribers with the data
601566
602567
- A simple module with publish and subscribe function can be exposed to achieve such functionality
603568
- List of subscribers can be maintained in an array and can be invoked in loop on each publish

0 commit comments

Comments
 (0)