Sobes.tech
Junior — Senior

Single-pass sequence filter excluding the last N elements

livecode

Task condition

Implement the FilterLast function that returns all elements of the source stream except for the last N elements. The source stream cannot be traversed more than once, and it cannot be fully stored in memory because its length is unknown in advance (calling source.Count() would require a full enumeration). Requirements for the solution:

  • Use a single-pass streaming algorithm;
  • The value of N is significantly less than the length of the sequence;
  • Additional memory is only allowed for a small number of elements (a buffer of size N).
IEnumerable<T> FilterLast<T>(IEnumerable<T> source, Int32 skipCount)