Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic: runtime error: index out of range , when there are ~ 9k elements in array. #17910

Closed
ralic opened this issue Nov 14, 2016 · 6 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@ralic
Copy link

ralic commented Nov 14, 2016

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.7.3

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/usr/local/opt/go/libexec/bin:/usr/local/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7.3/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d6/lnrxqp194zn2c1g3_02j1pdh0000gn/T/go-build196018249=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

source code --
http://tinyurl.com/hnzm7nd

What did you expect to see?

https://www.hackerrank.com/challenges/ctci-array-left-rotation
All test case passed.

What did you see instead?

failed in test case 6, 8, 9

[Error Messages]

panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x9a840, 0xc42000a110)
	/usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1a1
main.main()
	/Users/raliclo/work/@Netbeans/HackerRank_Practices/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation.go:25 +0x5ff
exit status 2
@odeke-em
Copy link
Member

@ralic the error you are getting seems to come from your code, according to the stack trace ie
on line 25, which is from https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation.go#L25

line2 := strings.Split(lines[1], " ")

could you confirm that you actually get more that one line as a sanity check in your code?

@namKolo
Copy link

namKolo commented Nov 14, 2016

I have the same issue with @ralic
@odeke-em With test cases, the second line always exists .
P/S sorry, that was my mistake. I found the issue :)

@odeke-em
Copy link
Member

P/S sorry, that was my mistake. I found the issue :)

@namKolo, so you found the issue in your code? Does this issue still stand then?

@ralic and @namKolo could y'all perhaps reproduce this by making a repository with the inputs that caused the failure? Otherwise we won't be able to diagnose what's going on easily.
Thanks.

@ralic
Copy link
Author

ralic commented Nov 16, 2016

@emmanuel,

Input :
https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Input_for_Array_Left_Rotation.txt

Output :
https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Output_for_arrays_left_rotation.txt

Failed Code for this large array :
https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation.go

Working Code for the same input :
https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation_2.go

------------------------------------------------------------------------
---
Phone: 408-609-7628
Email: RalicLo@gmail.com
------------------------------------------------------------------------
---

--

On Tue, Nov 15, 2016 at 8:00 PM, Emmanuel T Odeke notifications@github.com
wrote:

P/S sorry, that was my mistake. I found the issue :)

@namKolo https://github.com/namKolo, so you found the issue in your
code? Does this issue still stand then?

@ralic https://github.com/ralic and @namKolo
https://github.com/namKolo could y'all perhaps reproduce this by making
a repository with the inputs that caused the failure? Otherwise we won't be
able to diagnose what's going on easily.
Thanks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#17910 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AGbukq4AUZEJPRYbQ06ehJXGQfwi7k1_ks5q-n_EgaJpZM4KxC7X
.

@quentinmit quentinmit added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 16, 2016
@odeke-em
Copy link
Member

@ralic thank you for the repro.
Okay so the problem is that bufio.Scanner returns a bufio.ErrTokenTooLong error after that scan loop, yet that error wasn't being checked for early enough (it was checked way too late down the program) hence the runtime panic because you only got one line instead of two. The diff below
catches the error

--- orig.go 2016-11-16 20:08:58.000000000 -0800
+++ fixed.go    2016-11-16 20:23:51.000000000 -0800
@@ -6,6 +6,7 @@
    "os"
    "fmt"
    "strings"
+   "log"
    "strconv"
 )

@@ -21,6 +22,10 @@
        lines = append(lines, scanner.Text())
    }

+   if err := scanner.Err(); err != nil {
+       log.Fatalf("err reading standard input: %v", err)
+   }
+
    line1 := strings.Split(lines[0], " ")
    line2 := strings.Split(lines[1], " ")
    //fmt.Println(line1)
@@ -37,11 +42,6 @@
        }
    }

-   if err := scanner.Err(); err != nil {
-       fmt.Fprintln(os.Stderr, "reading standard input:", err)
-   }
-
-
    // Performance Report
    //elapsed := time.Since(start)
    //fmt.Println("Time Elapsed:", elapsed)

and the example on bufio.Scanner also shows how to catch that error https://golang.org/pkg/bufio/#example_Scanner_lines.

To get your program to deal with the multitude of elements that are in the single line, I instead used bufio.NewReader and then Reader.ReadString('\n') like this https://play.golang.org/p/WGfyqmVk79.

I don't see anything more that we can do on the Go side, except encourage checking errors early, otherwise this isn't a bug in the Go stdlib packages.

@ralic
Copy link
Author

ralic commented Nov 17, 2016

Hi Emmanuel,

Thank you so much for catching my need, I would also start using
bufferedReader (newReader) in most cases, instead of newScanner for
data array that may have unknown size.

If the case of fixed size array, it seems to be a good workaround using
make() to initiate it and then get each value by fmt.Scan() , which works
very well to replace the scanner with the flexibility to define the data
input type either to be double/string ... for the large array as well.

Sincerely,
Ralic

--

On Wed, Nov 16, 2016 at 8:44 PM, Emmanuel T Odeke notifications@github.com
wrote:

Closed #17910 #17910.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#17910 (comment), or mute the
thread
https://github.com/notifications/unsubscribe-auth/AGbukvookgbC3y7c0q34mm2QJ1LDs7Kdks5q-9ulgaJpZM4KxC7X
.

@golang golang locked and limited conversation to collaborators Nov 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants