-1

Solving Hackerrank problem and getting error index out of bound.

This is a C# coding. The problem statement can be found here https://www.hackerrank.com/challenges/append-and-delete/problem

class Solution {
    // Complete the appendAndDelete function below.
    static string appendAndDelete(string s, string t, int k) {
        int counter = 0;
        int n = s.Length;
        int m = t.Length;
        for(int i = 0; i <= Math.Min(n,m); i++)
        {
            if (s[i] == t[i])
            {
                counter++;
            }
            else
            {
                break;
            }

        }
        int a = n - counter;
        int b = m - counter;
        if (a + b <= k){
            return "Yes";
        }
        else{
            return "No";
        }
    }

Upon executing I am getting this error

[ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Index was outside the bounds of the array. at Solution.appendAndDelete (System.String s, System.String t, System.Int32 k) [0x0001b] in solution.cs:24 at Solution.Main (System.String[] args) [0x0002c] in solution.cs:53

The indices seem to be in range need help on this.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Mr. A
  • 103
  • 1
  • 2
  • 17
  • 2
    The error itself is self explanatory. You say the indices "seem to be" in range....but you wouldn't be getting that error if they were, would you? Seems like you just need to run this code with the debugger attached and see what happens. – mason Sep 12 '19 at 17:55
  • 1
    `The indices seem to be in range` Why do you claim this? `i <= ...`??? – LarsTech Sep 12 '19 at 17:56
  • While we're happy to help with whatever coding problems you might have, the problem you are trying to solve must be stated on this site, not only on an external site. – Heretic Monkey Sep 12 '19 at 18:01
  • I tried this in VS2017 and it did not show any error. – Mr. A Sep 12 '19 at 18:02
  • Duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/q/20940979/215552) – Heretic Monkey Sep 12 '19 at 18:02
  • You won't see runtime errors in the code editor - only compile errors. Your code compiles fine, but fails at runtime when it tries to access an invalid index. – Rufus L Sep 12 '19 at 18:02

3 Answers3

2

The problem is that you're using <= to compare the iterator against the smallest array's Length property. But since collections in c# are 0-based, you are trying to access an index that doesn't exist (the last index is always Length - 1).

Instead, use the < operator:

for (int i = 0; i < Math.Min(n, m); i++)
{
}

Also, it's usually not a good idea from a performance standpoint to call a function on each iteration if the return value of the function never changes. Instead, you should save the value of Math.Min() first, and then execute your loop:

var smallestLength = Math.Min(n, m); 

for (int i = 0; i < smallestLength; i++)
{
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Suppose, s="hackerhappy" t="hackerrank" then min length is 10 and as per code it should break it after 5 i.e length 6 then how come it is going out of the bound?? plz explain thanks in advance – Mr. A Sep 12 '19 at 18:16
  • But it is **NOT** going out of bounds when those strings are used. When I run your code in website, "Sample Test case 0", which is the one that used those strings, is the only one which passed. When I modify your code with my suggestion above, all testcases pass. – Rufus L Sep 12 '19 at 18:22
  • that we can see that after making those changes it works but if index is not going out of bound then why it is throwing that error, I want to know why it is throwing that error? – Mr. A Sep 13 '19 at 04:21
  • 1
    It is not throwing that error with the strings you mentioned. The other testcases are failing with that error because you are attempting to access an index outside the bounds of the array. Try running your method with the strings from one of the other two testcases, set a breakpoint in your code, and watch what happens. In those two testcases, the loop does run to the end of the shortest string, and then tries to access a last index that doesn't exist. – Rufus L Sep 13 '19 at 04:25
  • 1
    The testcases that failed with that error had the input: `"aba", "aba", 7` and `"ashley", "ash", 2`. In both cases, the length of the shortest string is 3, and in both cases the first 3 characters match. So in your code, the loop continues since you have `i <= 3` as a condition, and the failures happen when trying to access `"aba"[3]` and `"ash"[3]`, since the largest index in both these strings is `2`. – Rufus L Sep 13 '19 at 04:40
1

you are off by 1.

 for(int i = 0; i < Math.Min(n,m); i++)

a b c

1 2 3

length is 3

but last index is: 2

a b c

0 1 2

terrencep
  • 675
  • 5
  • 16
1

<= should be <.

For example, a string of length 9 has maximum index of 8

Julian
  • 33,915
  • 22
  • 119
  • 174