jaswanthi's blog

By jaswanthi, history, 9 years ago, In English

I am confused when dealing with arithmetic around array indices, majorly around the corner cases.

For ex:

  • Finding word in text ( search of needle in haystack). In following, it should be i <= hayStackTill instead of <
    public int search(char[] haystack, char[] needle) {
            int hayStackTill = haystack.length - needle.length;
            for(int i = 0; i < hayStackTill; i++) // it should be i <= hayStackTill instead of <
    }
  • Given the size of array and count from last, Find the index from start
   int index = a.length - countFromLast; // it should be instead a.length - countFromLast + 1
  • Also, here https://youtu.be/KMBe8Lhhti8?t=12319 Mimino suggests to use +7 , because there is chance of index out of bounds. which is needed only if you don't estimate array indices properly.

Can anyone help with some practice problems around this ( Also any suggestions are welcome).

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?