We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
While this solution works and this forum has good explanations of why it works, the results don't make sense if the value entered for k is greater than the value for n.
Of course, many will say the constraints include "2 ≤ k ≤ n". That's what we should expect in the input. So that's what the solutions are designed to work with. But what should the code do when k > n?
I think the code should check that the values are within reason and either give an error or do something appropriate. In the case of this specific problem, k is supposed to be the upper limit of the number to be found. I think it's acceptable to set k equal to n and continue the program as before.
I wonder why HackerRank doesn't include bad data in its tests. The ability to code a solution to a problem isn't an accurate representation of real world coding. Solutions should be able to handle bad data as well. I think that by not including this in challenges, new coders may not learn to write fault-tolerant code.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 29: Bitwise AND
You are viewing a single comment's thread. Return to all comments →
While this solution works and this forum has good explanations of why it works, the results don't make sense if the value entered for
k
is greater than the value forn
.Of course, many will say the constraints include "2 ≤
k
≤n
". That's what we should expect in the input. So that's what the solutions are designed to work with. But what should the code do whenk
>n
?I think the code should check that the values are within reason and either give an error or do something appropriate. In the case of this specific problem,
k
is supposed to be the upper limit of the number to be found. I think it's acceptable to setk
equal ton
and continue the program as before.I wonder why HackerRank doesn't include bad data in its tests. The ability to code a solution to a problem isn't an accurate representation of real world coding. Solutions should be able to handle bad data as well. I think that by not including this in challenges, new coders may not learn to write fault-tolerant code.