Stock Exchange Losses puzzle discussion


#1

Feel free to send your feedback or ask some help here!



Bug in "Stock Exchange Losses" puzzle - bad input?
#2

Hi, its strange, for this one, test5, I executed code on my local and I printed the expected result.
But on CodinGame, it seems either the script timed out before the end or the input was truncated.

I’am using javascript code.


#3

Brute force approaches and simple optimizations time out even in C++, but with a clever trick I could complete in time using both languages I tried with, C++ and Python.


#4

Yes, solution is to be clever… I succeed by using tricky optimisation…


#5

I’m having some issues with this during submission. I have solved the problem, all of my tests pass, but when I submit it the first test fails. The first test during submission doesn’t seem to be much different than the first test that is provided while writing the solution. It seems to me that my solution is solving it correctly but that it’s being marked as incorrect. Has anyone else had this problem?


#6

I get weird results for test5. It won’t pass and I can’t capture the entire input because there are so many numbers. All my other tests pass.


#7

Can I get the input data for test5? All my tests pass except for this one and I can’t seem to figure out what is going wrong. My code seems correct to me. Alternately I can post my code if you can tell me if anything is wrong then I will go back to the drawing board. I don’t want to be told how to fix it I want to do that on my own…


#8

Your code is too slow. I got exactly the same problem, and it took me some time to understand what was happening.

In short, you should find a solution in O(n), while the trivial solution (probably the first idea for most people) is in O(n^2).

It is pity that the error is not more explicit. First, the text of the problem should indicate clearly that the expected solution must be optimal. For most other problems, speed is never an issue, so I guess it is unexpected to fail due to a slow program. Secondly, the error should be much more explicit.


#9

My code is O(n) and it runs to completion. I get this output on Test 5: Does this mean I am overflowing an int type?

Console output
-1073734157

Fail
Found: “-1073734”
Expected: “-1073730”


#10

What are the two numbers in the input that make up the loss calculation?


#11

What are the two numbers that make up the loss calculation?


#12

You can’t overflow your int, because of the constraint 0 < v < 2^31. However, this output is exactly minimum - maximum of test 5 values so there’s something wrong in your algorithm.

Also, inputs and expected outputs for the IDE tests are given at the end of the puzzle statement.


#13

I finally figured this one out. The problem was in my logic. What a clever puzzle.


#14

first test get error even if i print real answer (Python)


#15

Do you print it along with an end of line ?


#16

I must be somehow fundamentally misunderstanding the requirements on this one. All 5 of the tests pass in the IDE, but when I submit, then grading test #1 fails:

Case (4 3 5 2 1 6) -> -4 (300 pts)

Looking at the data, it seems to me that my algorithm is working correctly. The largest loss in this dataset is from 5 (3rd entry) to 1 (5th entry). My program prints out “-4”, as shown. Is this not the correct answer?

  • dan

#17

Never mind. I figured out the problem through trial and error (lots of submissions). There must be an extraneous space in the input of the first validation test for that problem. I had been just doing a split on " " (single space) to get the input integers and then looping through the result, but that made me overflow my array (more than n results from the split operation).

Once I put some defensive logic into this part of my program, then I’m able to pass all the validation tests. Unfortunately, since I can’t get any debugging info from the validation tests, it’s hard for me to know exactly what about the input my program didn’t like.

  • dan

#18

I found bag in this task (in first test) n==5 but array has 6 numbers


#19

Wow. Was using LINQ’s TakeWhile and Last methods, everything was fine, but fifth was failing.

It forced me to think a bit what i get in those number ranges :smiley:


Bug in "Stock Exchange Losses" puzzle?
#20

I agree with your assessment, @alexxx_was. I was wrong about extra spaces in the “vs” string. The first validation test has n=5. To test this theory, I submitted the following Java:

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    in.nextLine();
    String vs = in.nextLine();
    System.out.println(Integer.toString(-(n-1)));
}

This code passes the first test only with “correct” output: “-4”. The test is providing invalid input.

  • dan