Zoho Interview Question
StudentsCountry: India
Interview Type: Written Test
public int findKth(int k)
{
if(k<=0)
{
return -1;
}
if(k==1)
{
return 4;
}
if(k==2)
{
return 7;
}
int[] arr=new int[k+1];
arr[1]=4;
arr[2]=7;
for(int i=3;i<arr.length;i++)
{
arr[i]=i%2!=0?(arr[(i>>2)]*10)+4:(arr[(i>>2)-1]*10)+7;
}
return arr[arr.length-1];
}
//Time complexity: O(K). Space Complexity: O(K)
I'm not sure I understand your question. As it's phrases, you are getting the index input for an array of "4,7,44,47,74,77" Which doesn't seem a likely interview question. Can you clarify?
- Anonymous July 03, 2016