Fun Size Bytes

  • Who I Follow
  • "Favorite Part" posts (starting at oldest)
  • Favorite Part (starting with most recent)
  • Contact Me
  • ask me anything
  • rss
  • archive
  • Overly complex code interview answer [NERD WARNING]

    cutlerish:

    I got a question during one of my interviews intended to determine my level of knowledge of Linux command-line and command-line tools.

    Given a file called file with 16 names each on its own line, print out only the middle eight names.

    The simplest answer, of course is: head -12 file | tail -8

    However, we had just been discussing my thesis—for which I had done a lot of bash scripting—and instead of thinking of that first, my answer came out:

    count=0; for line in `cat file`; do let count=count+1; if [[ "$count" -gt "4" ]]; then if [[ "$count" -le "12" ]]; then echo $line; fi; fi; done

    “Nobody I recall has whipped out bash scripting quite so readily for this. Can you think of another way?” asked the interviewer.

    Only then did I think of using head and tail. I either impressed or confused him. Possibly both. I did get the job, though.

    I’m glad you got the job, but you didn’t get the simplest answer :-)

    Given this file ‘file’ with 16 names:

    % cat file
    1 Andrew
    2 Brian
    3 Chris
    4 Dave
    5 Ethan
    6 Frank
    7 Greg
    8 Hannah
    9 Isaac
    10 Joseph
    11 Kelly
    12 Lauren
    13 Mike
    14 Nathan
    15 Orville
    16 Peter
    

    You can get the middle 8 with one sed command and no fork:

    % sed -n '5,12p' file   
    5 Ethan
    6 Frank
    7 Greg
    8 Hannah
    9 Isaac
    10 Joseph
    11 Kelly
    12 Lauren
    

    This has been a nerd-off.

    Source: cutlerish
    • October 27, 2012 (2:52 pm)
    • 63 notes
    Previous Post Home Next Post
    1. curiosojorge likes this
    2. bachlobster likes this
    3. crustyjuggler72 likes this
    4. cewrew likes this
    5. ayyjam likes this
    6. jdhtwo likes this
    7. scholvin likes this
    8. justalittletaste likes this
    9. cutlerish likes this
    10. kodinlanewave reblogged this from tj
    11. kodinlanewave likes this
    12. steelopus likes this
    13. cpillsbury likes this
    14. abundanceofcalm likes this
    15. badgopher likes this
    16. tj reblogged this from cutlerish and added:
      I’m glad you got the job, but you didn’t get the simplest answer :-) Given this file ‘file’ with 16 names: % cat file 1...
    17. tj likes this
    18. folding-space likes this
    19. whimpernotbang said: I feel like my Systems Programming class -should- allow me to understand this. No wonder I’m about to fail it…
    20. solaceamongstars likes this
    21. tymethiefslongerthoughts likes this
    22. tarpo likes this
    23. friedlinguini likes this
    24. thekiltedginger likes this
    25. whimpernotbang likes this
    26. turkofgallifrey reblogged this from cutlerish
    27. darthtravis likes this
    28. aidiera likes this
    29. myheartonthefloor likes this
    30. felinefaytale likes this
    31. mathcat345 likes this
    32. mtgfan likes this
    33. nicknameless likes this
    34. imabtastic likes this
    35. ladameblanc said: I live knowledge sharing like this. I used to do it for EE Tech questions. Love it!
    36. mayberedbird likes this
    37. ladameblanc likes this
    38. cannelledusoleil likes this
    39. misaimedjaimes likes this
    40. rosiedoestumblr likes this
    41. rand0mflora likes this
    42. mikerastiello likes this
    43. davesnothere likes this
    44. justblamechris likes this
    45. andsoisyourface likes this
    46. lukitari likes this
    47. unstimulating said: Bash is a great language! So underrated yet so damn powerful!
    48. underwhelmed likes this
    49. spiralingsidewayz likes this
    50. aesho likes this
    51. Show more notesLoading...
© 2008–2013 Fun Size Bytes