See if you can spot the mistake in this code?


Heres the code…

public void fillSlots(String s, ScriptEntity.ScriptEntityType t, int startHour, int startMinute, int frequency)
{
    frequency = frequency / MINUTES_IN_ONE_HOUR;
    if (frequency == 0) {
        return;
    }

    int iX = startMinute / MINUTES_IN_ONE_SLOT; 
    // X index: 0 minute -- iX (0); 15 minute -- iX (1);
    // 30 minute -- iX (2); 45 minute -- iX (3).
    int iY = startHour % Y_INDEX; // Y index

    for (int i = iY; i < Y_INDEX; ) {
        fillSlot(s, t, iX);
        i =+ frequency;
    }
}

Hint: Infinite loop !!!

Advertisements
See if you can spot the mistake in this code?

7 thoughts on “See if you can spot the mistake in this code?

  1. Ganesh says:

    I don’t know the value of Y_INDEX. I am assuming it is something like 24 (Hours in a day). Value of i is incremented inside the for loop. frequency is checked for 0. So how can it be in an infinite loop ?

  2. I don’t know the value of Y_INDEX. Assume its 24 (hours in a day). Frequency is checked for 0, value of i is incremented inside for loop (i+=frequency). Unless I am missing something big, I can’t find any infinite loops here.

    What am I missing ?

  3. faisalferoz says:

    @Ben, very true. This highlights one more mistake in the cod. Altough getting the frequency -ve is pretty rare but still we should have provisions to avoid these situations in the code when the fix can be as simple as changing the condition from == 0 to <= 0. Thanks

    1. Faisal Feroz says:

      Read the code like this:

      i = (+frequency)

      The problem is the the value of i never gets incremented, it always gets assigned the same value which results in an infinite loop.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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