Angel \”Java\” Lopez on Blog

May 10, 2013

TDD Kata (4): Lawnmover

Previous Post
Next Post

In the previous post, I commented about by experience at pre-round at Google Code Jam.

The exercise B was Lawnmover:


Alice and Bob have a lawn in front of their house, shaped like an N metre by M metre rectangle. Each year, they try to cut the lawn in some interesting pattern. They used to do their cutting with shears, which was very time-consuming; but now they have a new automatic lawnmower with multiple settings, and they want to try it out.

The new lawnmower has a height setting – you can set it to any height h between 1 and 100 millimetres, and it will cut all the grass higher than h it encounters to height h. You run it by entering the lawn at any part of the edge of the lawn; then the lawnmower goes in a straight line, perpendicular to the edge of the lawn it entered, cutting grass in a swath 1m wide, until it exits the lawn on the other side. The lawnmower’s height can be set only when it is not on the lawn.

Alice and Bob have a number of various patterns of grass that they could have on their lawn. For each of those, they want to know whether it’s possible to cut the grass into this pattern with their new lawnmower. Each pattern is described by specifying the height of the grass on each 1m x 1m square of the lawn.

The grass is initially 100mm high on the whole lawn.

I solved it, see:

As usual, the commit history by test:

I could solve the first small set provided by Google. But I failed with the big set, because my algorithm was not efficient enough. The big set should be solved in 8 minutes, and if you cannot, there is no other chance. My initial algorithm (see comments) attacked the largest number in cells. My second algorithm switched to start with the minimal numbers (my first guess was that both algorithms had the same time, but I was wrong). I learnt: some problems in Google are hard to test by hand, we don’t have the use case (input and expected output), that is the main difficult.

More katas are comming.

Keep tuned!

Angel “Java” Lopez


  1. […] Previous Post Next Post […]

    Pingback by TDD Kata (3): Tic-Tac-Toe-Tomek | Angel "Java" Lopez on Blog — May 10, 2013 @ 9:53 am

  2. […] TDD Kata (4): Lawnmover […]

    Pingback by TDD Kata (4): Lawnmover | MVPs de LATAM — May 16, 2013 @ 5:22 pm

  3. […] Previous Post […]

    Pingback by TDD Kata (5): Tree Search | Angel "Java" Lopez on Blog — June 30, 2013 @ 3:29 pm

  4. […] Previous Post […]

    Pingback by TDD Kata (5): Tree Search | MVPs de LATAM — July 2, 2013 @ 3:04 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at


Get every new post delivered to your Inbox.

Join 57 other followers

%d bloggers like this: