1. we can search at index 0,1,2,4,8,16....
2. at the point we get getItemAt(x) == 1, our required index at x/2 < ans_index <= x
3. from (x/2)+1 to x we can do binary search and can find the required index

For n=4.. you have to take 1,2 or 3.. So for n=1 , you have to take nothing.. so C(1) = 0.. C(2) = 1 ( {1, 1}), C(3) = 3 ({1,1,1},{2,1},{1,2})... and by your algo: C(4) = C(3) + C(2) + C(1) which gives 4(3+1+0) .. And also C(5) = 15 and by your algorithm it is 13.. a wrong answer..

From 1 to 50
Let the coin be at the initial position and the number of steps moved is zero
>>the next six step or cell(2,3,4,5,6,7) can be reached with minimum 1 dice roll
>>mark the cell with 1
>>if any of these six cell has lower point of ladder then mark upper point of ladder with 1
>>continue the above steps with MAXIMUM upper point of the ladder and mark the further cell with current weight(here it is 1) with 1

1. calculate number of space between words: here it is 2
2. calculate number of space to be distributed : here 15-11 = 4
3. each space between words will have additional space: 4/2 and either first or last space will have 4%2 (here 0, so distribution is common) additional space

the element in the subset should be consecutive.. that is sub-array..
if it is not consecutive, then we can take only positive elements from the array which is not at all a problem to post and discuss here. ..

Use One pointer to the list and a queue of size 5:
1) traverse to the 5th node
2) insert the element at the queue
3) if the queue is full, take rear element out and insert the new element at the front
4) repeat it till the pointer reaches null
5) the rear pointer of the queue contains the 5th element from the tail of the list..

for simplicity : i am explaining for sorting 10 numbers with memory size 2
a) find the minimum element and its index in one full traverse from i to (total_numbers -1) [here it is 8]for (i+1)th iteration
for ex: {5,8,1,3,9,10,6,4,2,7} , store {2,1} where 2 is the index and 1 is the element for 1st iteration
b) swap(input[min_index],input[i]) where (i+1)th iteration
here,
swap (input[2],input[0]) for the 1st iteration
c) repeat a) and b) for 8 times .. as for 9th iteration no swaping will be there and elements are sorted..

take {1,2,3,1,4,2,3,7,5,6}
range : only one range.. 1...7
now..
first consecutive: 1,2,3 as the next 1 repeated and cannot be taken into account and length = 3
next..
1,4,2,3,7,5,6.. and length = 7 greater than previous one.. so update it..

if there is any out of range value eg: 10.. counting is stopped and checked for consecutiveness(whether '1' in array[] is continuous) and previous count..

Note: you have to update the count only the array[] is of the form {1,1,1,0,0,0..} not {1,0,1,0,1,1} because numbers should be consecutive

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.