Samsung Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
If my understanding is correct, following is the way to solve this.
Taking a simple example of 2 numbers spread over 3 place.
array A = {1,2}; n=3
say the number is: xyz
I see that repetition is allowed, as per the example.
when n=3, we have to take into account n=2 and n=1 based on what I see from the example given in the question.
x can have 1 or 2; y also can have 1 or 2; z also can have 1 or 2.
So the possibilities of all types of 3 digit numbers is: 2*2*2 = 8
Same way, possibilities of all types of 2 digit numbers is 2*2=4
Same way possibilities of all types of 1 digit number is 2
In this case answer would be 14.
Going by this, following snippet would reap the result.
numOfPlaces = 1
while(numOfPlaces<=n) {
result = result + array.length ^ numOfPlaces;
numOfPlaces++;
}
Note: I have not tested the while loop. but conceptually, I believe I have given a fair answer based on the question.
Correct me if wrong.
Question is not clear. Can you please rephrase the question and edit the question. When n > 0, doesn't it imply that it can be greater than 4?? One more doubt I have is given example for n=5
- prudent_programmer March 05, 20181,11,111,1111,11111,12341 , the numbers 222, 2222, 333, 3333, .... are also valid right?