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);
let answer = 0;
Obviously it’s a pseudo code but I think you will get the idea.
Here is solution for the Sum of digits:
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
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.
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.