Friday, October 28, 2011

A coworker of mine was asking me one of his favorite interview questions.  It basically goes like this:

"Write me something that will change any file ending in .html to .htm"

Okay, simple enough, I write out a quick bash loop and hand it to him, then he says:

"Okay, now let's say the directory has spaces in the name"

A bash for loop will create an array by splitting on spaces, so something like this would break:

for file in `find ./ -name *.html`
do
  mv $file $newfile
done

If you had a directory named "my directory" you'd have an entry for "my" and an entry for "directory/blah.html" which wouldn't work.

The solution is to do some "while reading this line" bullshit in bash, but my solution was much more elegant, throw away bash and use ruby:

irb(main):001:0> Dir.glob("**/*.html").each do |fileName| system( "mv \"%s\" \"%s\"" % [fileName,fileName.gsub( /\.(html)$/,".htm" )] ) end

This question is designed to see if you can think around complicated scenarios.  However, in the wild I'd never trust bash to do the right thing.

Bash is dumb, ruby is elegant perfection.  I guess I would have failed that interview question. ;)

3 comments:

  1. find . -type f -name "*.html" | sed 's/l$//' | xargs -I% mv %l %

    I think mine's more elegant :-D

    ReplyDelete
  2. I got asked that more than once during interviewing earlier this year. I don't know why it's a popular question I don't think it's a good question but here is how I answered it - not as elegant as ryan but it works..

    find -type f -name "*html" | while read line; do export NEWNAME=`echo $line | sed s'/html$/htm/'; mv "$line" "$NEWNAME";done

    I always echo the command out before doing the mv to make sure it looks right.

    Also I added the $ to the end of html just in case, I haven't tested that as much(and wasn't included in my interview answers).

    Though the people that asked me gave me a passing grade for my answer.

    When I interview folks(especially more senior) I like to just have a conversation with them:
    - Talk about some big project you worked on
    - What is your proudest accomplishment

    Then I dive deeper into their responses and see what they know.

    One developer at my current gig highly recommended some operations guy from his past. He spoke about this guy like he was gold.

    Well he wasn't. He was certainly a very nice person, willing to work hard and get stuff done(and I'd be totally willing to hire him for a junior role - I wasn't sure what kind of role we were hiring for). But he did not have much experience (felt like myself back in 2002). His vmware experience was limited to running it on his desktop/laptop, and didn't really grasp the concepts of virtualization in general. He had no storage or networking experience.

    When he told me about a complex problem he worked on, it certainly sounded complex(it was a networking issue related to BGP if I remember right), until he told me that he fairly quickly handed the issue off to a network engineer and they figured out the solution(forgot what it was).

    BTW your co-worker that you mention, when I interviewed him he didn't really impress me much (and I was even less impressed after he started). But I was to the point where I didn't care either way anymore, even if he didn't do anything(which turned out to be true), we still got along fine more or less.

    BTW I'm going to be in Seattle next weekend for some COWGIRLS action among other things, if your up for some drinks come out to cowgirls on fri or sat (18th/19th) after 9pm I plan to be there both nights.

    Going to Atlanta early next month to do some real data center work, finally, been too long. 10gig switching, vmware, 3PAR storage.

    ReplyDelete
  3. Big benefits for Diablo 3 players to greet Halloween, more coupon code for D3 Gold will help you save more money and time to enjoy the game.

    ReplyDelete