I was asked the following question in an interview. I don’t have any idea about how to solve this. Any suggestions?
Given the start and an ending integer as user input,
generate all integers with the following property.Example:
123 , 1+2 = 3 , valid number 121224 12+12 = 24 , valid number 1235 1+2 = 3 , 2+3 = 5 , valid number 125 1+2 <5 , invalid number
A couple of ways to accomplish this are:
Test every number in the input range to see if it qualifies.
Generate only those numbers that qualify. Use a nested loop for the two starting values, append the sum of the loop indexes to the loop indexes to come up with the qualifying number. Exit the inner loop when the appended number is past the upper limit.
The second method might be more computationally efficient, but the first method is simpler to write and maintain and is O(n).
I don’t know what the interviewer is looking for, but I would suspect the ability to communicate is more important than the answer.