-2

This Question was asked in the interview. What is best way to find 'the' from the file having 30k lines? You should make sure it is fast and required less memory.

Edit-1: I meant the efficient way to process the file. I answered him to read one line at a time and processor it using thread from threadPool.

1 Answers1

1

Hard to tell what the "best" solution is if there´s given no further criteria how best is defined. It might be

cat file.txt | nl | grep "the"

if the goal is to invest least programmer hours. As the comments already pointed out the bottleneck will be the time required for disc access. The file size of 30k lines should still be less than 10 MB so just read it in to one memory chunk to avoid additional disc reads. Then do some profiling on a single threaded search to check if it is worth the effort to make use of parallelization, as this also comes with some overhead for thread spawning and coordination and higher code maintenance costs.

J. Mueller
  • 121
  • 1
  • 7
  • Why not `grep -n "the" file.txt`? – Zereges Feb 11 '19 at 14:14
  • @Zereges Because my intention just was to illustrate the point that rather simple solutions can also be good ones regarding developer times and did not want to dig into `man grep`. – J. Mueller Feb 11 '19 at 14:30