This actually was an interview question from VanHack, a platform for remote digital work.
They provide you with 2 assessments and give you 24 hours to solve it.
Even thou i couldn't pass all the test cases of the first assessment, it teach/refresh a lot of concepts from Data Structures, Algorithms, Buffers & Stream over traditionally file read.
A buffer is an area of memory.
A simple visualization of a buffer is when you are watching a YouTube video and the red line goes beyond your visualization point: you are downloading data faster than you're viewing it, and your browser buffers it.
In order words store data to be process later
Traditionally, when you tell the program to read a file, the file is read into memory, from start to finish, and then you process it.
Using streams you read it piece by piece, processing its content without keeping it all in memory.
Streams provide two major advantages:
- Memory efficiency
- Time efficiency