Created
June 30, 2025 08:20
-
-
Save jsuryahyd/6b404a52c89921b40fc1493da0797216 to your computer and use it in GitHub Desktop.
Array Iterator and Interleaver - iterative and queue approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interview question by Fluxon (round 2) | |
//iterator interface | |
// next(): number; | |
// hasNext(): boolean; | |
class ArrayIterator<T> { | |
nextIdx = 0; | |
arr: T[] = []; | |
constructor(arr: T[]) { | |
this.arr = arr; | |
} | |
/** | |
* @returns boolean | |
*/ | |
hasNext() { | |
if (this.nextIdx <= this.arr.length - 1) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @returns number | |
*/ | |
next() { | |
return this.arr[this.nextIdx++]; | |
} | |
} | |
const it = new ArrayIterator([1, 2, 3]); | |
// while (it.hasNext()) { | |
// console.log(it.next()); | |
// } | |
/** | |
* | |
*/ | |
class Interleaver { | |
private iteratorIdx: number = 0; | |
constructor(private iterators: ArrayIterator<any>[]) {} | |
/** | |
* | |
* @returns | |
* @description | |
* - returns [iteratorIdx].hasNext() | |
* if(last iterator){ | |
* if(!hasNext) return false | |
* else nextiterator | |
* } | |
* return false | |
*/ | |
hasNext(completedArraysCount = 0): boolean { | |
if (completedArraysCount === this.iterators.length) return false; | |
const hasNext = this.iterators[this.iteratorIdx].hasNext(); //this iterator is over, go to next one | |
if (!hasNext) { | |
this.incrmentIterator(); | |
return this.hasNext(++completedArraysCount); | |
} | |
return true; | |
} | |
// // alternatively iterative approach | |
// hasNext(): boolean { | |
// let complextedArraysCount = 0; | |
// let currIdx = this.iteratorIdx; | |
// while (complextedArraysCount < this.iterators.length) { | |
// if (this.iterators[currIdx].hasNext()) { | |
// this.iteratorIdx = currIdx; // Update iteratorIdx if we find one with next | |
// return true; | |
// } | |
// currIdx = (currIdx + 1) % this.iterators.length; | |
// complextedArraysCount++; | |
// } | |
// return false; // All iterators are exhausted | |
// } | |
next(): number { | |
const curr = this.iteratorIdx; | |
this.incrmentIterator(); | |
return this.iterators[curr].next(); | |
} | |
incrmentIterator() { | |
if (this.iteratorIdx === this.iterators.length - 1) { | |
this.iteratorIdx = 0; | |
} else { | |
this.iteratorIdx++; | |
} | |
} | |
} | |
class Interleaver2 { | |
constructor(private iterators: ArrayIterator<any>[]) {} | |
/** | |
* | |
* @returns | |
* @description | |
* | |
* - if firstIterator.hasNext() === true, return true | |
* - else checkj next iterator repeatedly (loop this), without pushing back the curr iterator. | |
*/ | |
hasNext(): boolean { | |
if (!this.iterators.length) return false; | |
while (this.iterators[0] && !this.iterators[0].hasNext()) { | |
this.iterators.shift(); | |
} | |
// alternatively recursive approach | |
// if (!this.iterators[0]?.hasNext()) { | |
// this.iterators.shift(); | |
// return this.hasNext(); | |
// } | |
return this.iterators[0]?.hasNext(); | |
} | |
/** | |
* | |
* @returns | |
* takes first iterator in array | |
* - push item back to array | |
* - return the iterator.next() | |
*/ | |
next(): number | undefined { | |
if (!this.iterators[0]) return undefined; | |
const curr = this.iterators.shift(); | |
this.iterators.push(curr as ArrayIterator<any>); | |
return (curr as ArrayIterator<number>).next(); | |
} | |
} | |
const i = new Interleaver([ | |
new ArrayIterator([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | |
new ArrayIterator([1, 2, 3]), | |
new ArrayIterator([4, 5, 6, 5, 10, 4, 8, 12, 13]), | |
new ArrayIterator([7, 8, 9, 100]), | |
]); | |
const j = new Interleaver2([ | |
new ArrayIterator([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | |
new ArrayIterator([1, 2, 3]), | |
new ArrayIterator([4, 5, 6, 5, 10, 4, 8, 12, 13]), | |
new ArrayIterator([7, 8, 9, 100]), | |
]); | |
let str1 = ""; | |
while (i.hasNext()) { | |
str1 += i.next() + ", "; | |
} | |
console.log(str1); | |
let str2 = ""; | |
while (j.hasNext()) { | |
str2 += j.next() + ", "; | |
} | |
console.log(str2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment