The Long Tail

less than 1 minute read

One of the questions in a technical interview I sat today was “Write ‘tail’ in Ruby”. I had a pen and paper and no access to Google. I think I came up with a reasonable solution but I realised how dependent I am on docs! It took me a long time to arrive at a solution when I couldn’t refer to Ruby API docs.

#!/usr/bin/env ruby

filename = ARGV[0]

end_marker = 0
offset_increment = 1000

lines = []
fh = filename
while lines.size < 10
  current_offset = end_marker + offset_increment, IO::SEEK_END) || fh.rewind
  buf = - end_marker).split("\n")
  lines.insert(0, *buf)
  end_marker += current_offset
puts lines.last 10

Hmm, it’s still kinda broken.