Skip to content

Commit aad1282

Browse files
author
hasibulislam999
committed
Design an Ordered Stream problem solved
1 parent b169121 commit aad1282

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Title: Design an Ordered Stream
3+
* Description: There is a stream of n (idKey, value) pairs arriving in an arbitrary order, where idKey is an integer between 1 and n and value is a string. No two pairs have the same id.
4+
* Author: Hasibul Islam
5+
* Date: 28/04/2023
6+
*/
7+
8+
/**
9+
* @param {number} n
10+
*/
11+
var OrderedStream = function (n) {
12+
// initialize array
13+
this.arr = [];
14+
// start pointer at index of 0
15+
this.p = 0;
16+
//set array to have length of n
17+
this.arr.length = n;
18+
};
19+
20+
/**
21+
* @param {number} idKey
22+
* @param {string} value
23+
* @return {string[]}
24+
*/
25+
OrderedStream.prototype.insert = function (idKey, value) {
26+
// push the value into array at index of idKey
27+
this.arr[idKey - 1] = value;
28+
// initialize result array
29+
let result = [];
30+
31+
// while the pointer has a value, push that value into the result array and advance the pointer
32+
while (this.arr[this.p]) {
33+
result.push(this.arr[this.p]);
34+
this.p++;
35+
}
36+
// return the result array which will either be empty if the pointer was null, or will have the chunks pushed into it
37+
return result;
38+
};
39+
40+
/**
41+
* Your OrderedStream object will be instantiated and called as such:
42+
* var obj = new OrderedStream(n)
43+
* var param_1 = obj.insert(idKey,value)
44+
*/

0 commit comments

Comments
 (0)