Keep getting Timed Out and Wrong answer randomly - First time user of Codechef


I am trying to run my code using NodeJS. This is my first time using Codechef and I have tried without success trying to get my first piece of code running but have failed every time over the past few months. Having figured out how to use process.stdin to get inputs , I am getting wrong answers and/or timed out randomly for both the ATM problem and Sum of digits Beginner problems.

ATM problem solution - Mostly Timed out, sometimes wrong answer alert shown too.
Sum of Digits solution - Mostly wrong answer shown.

Most likely because your solution is wrong (WA) or too inefficient (TLE).


I submit my solutions in NodeJs as well and sometimes it can be frustrating to see TLE. But do not let yourself to stop trying. The solution is there you just have to find it.
First I recommend aim for TLE (that means for solution is correct but not optimized) then you can start to change your code so that it makes much more efficient. You will learn a lot about Nodejs by doing it.
Also! never return an answer in the test loop!! that will stop your solution to check other tests. Use console.log(answer) almost everytime. If you want to break your code into simple components than you can do like this:
const n = input1
const arr = input2
const result = solve_my_solution(n,arr);
function solve_my_solution(n,arr){
let answer = 0;
return answer
Obviously it’s a pseudo code but I think you will get the idea.
Here is solution for the Sum of digits:
Kind regards,

1 Like

Thanks for the detailed reply. Helps a lot. I also fail to understand why a code which runs successfully for example custom input exceeds time limit on final submission. Shouldn’t the platform throw a warning in the test run itself? It would have been more helpful.

in the sum of digits, I am using map and split and reduce array methods and the answers are correct . But I do not know which functions result in TLE.

That’s the whole point of CP: to push your knowledge and intuition to the limit without giving you too many hints. Your code must be at the same time robust and extremely efficient for very large/complex inputs.
Personally I find it quite useful to generate some random input based on the problem constraints and test my (usually bad) solutions against it. It’s incredible how many bottlenecks and corner cases you can discover this way.

1 Like

Makes sense. Thank you for these inputs. Will need to pursue more efficient solutions and learn about them. I agree this approach would definitely lead to more robust code for large inputs.

How about this, same logic will work for c++ code but not for javascript code. I’m facing this issue most of the time for most basic problems. Actually I was a c++ programmer, but now I main javascript so decided to code with javascript. But o boi o boi, this doesn’t want to work.

I got surprise when I have 2 different error for the same answer in javascript. 1st error was wrong answer and 2nd error was Time limit exceed. The only difference was the code formatting. Now this isn’t python where indent matters but still I got 2 errors for the same program with different code format.