Sobes.tech
Junior — Senior

Handler and sorter of a potentially infinite stream of natural numbers

livecode

Task condition

Given: A sequence of natural numbers, which can be infinite.

Required:

  1. Implement a function that takes this sequence as input and, after viewing the first n elements, returns no more than n already viewed numbers, sorted either in ascending or descending order.
  2. Estimate the time complexity of the algorithm as O(f(n), m).

It is assumed that n significantly exceeds m (n ≫ m > 0), and also n ≫ m. The sequence has a length of n.

function seq(int $n): Generator
{
    while ($n > 0) {
        yield mt_rand(1, 1000);
        $n--;
    }
}

function solution(Generator $seq, int $n): array
{
}

print_r(solution(seq(1000), 10));