## Directi Interview Question Software Engineer / Developers

• 0
of 0 votes

He gave me an array of Integers, each integer allows me to make at max its value jumps. If i am at zero, i'm stuck i cannot move forword. He asked me to find,

1). If the last index was reachable from the first index.
2). Minimum number of jumps required to reach the last index, given any index as the starting index.
3). Total number of ways one can reach the last index.

I was asked to write a code for this.

ex: 1 3 5 8 9 2 6 7 6 8 9

initially at one i can make only one jump to 3, from 3 i can jump either 1 step reaching 5, or 2 steps reaching 8, or 3 steps reaching 9. Carrying on in the same way till i can hit the last index.

Comment hidden because of low score. Click to expand.
1
of 1 vote

Think of Dynamic Programming

Comment hidden because of low score. Click to expand.
1
of 1 vote

You can approach the problem in the following way -
Construct a Directed Graph where max No of jumps will be the no of leaving edges from that specific node.After constructing the Graph do a BFS tree traversal,u will get the min path and to find all possible path modify the standard BFS algo....

Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple code to find out if its reachable :

#include<stdio.h>
int jump(int a,int b)
{
return a+b;
}
void Func(int n,int a[n],int start,int last)
{
int i,k=0,temp;
while(k<n)
{

for(i=1;i<=a[k];i++)
{
temp=jump(k,i);
if(temp==last)
{
printf("Reachable\n");
return;
}
}
k++;
}
printf("Not reachable\n");
return;
}

main()
{
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Func(n,a,0,n-1);
}

Comment hidden because of low score. Click to expand.
0
of 0 votes

Your simple code does not work.. :P

Comment hidden because of low score. Click to expand.
0
of 0 vote

Backtracking + Recursion :

1). If the last index was reachable from the first index.
n=1
Take jumps of n, if current number is 0, backtrack and jump n+1 if possible, else backtrack. If you reach the end, then print reachable.
2). Minimum number of jumps required to reach the last index, given any index as the starting index.
n = biggest possible step
Take jumps of n, if current number is 0, backtrack and jump n-1 if possible, else backtrack. Keep a count of jumps.

3). Total number of ways one can reach the last index.
Aha ! variation of (1), when you reach end, do not exit from the program, rather increase the count. so you will get the count of all possible ways to reach the end.

Comment hidden because of low score. Click to expand.
0
of 0 vote

i think this code is working..

#include<stdio.h>
int n,s[100];
int min(int a,int b)
{

return(a<b?a:b);
}
int fun(int i,int count)
{
int j,max1=n+1;
if(i>=n)
{

return max1;
}
else
{
if(i==n-1)
{
return count;
}

for(j=1;j<=s[i];j++)
{

max1=min(max1,fun(i+j,count+1));
}
}
return max1;

}
main()
{
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
if((i=fun(0,0))==n+1)
{

printf("\ncould not reachable");
}
else
{

printf("\n\n\nreachable in %d steps ",i);
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int reachLastFromSomeIndex(int a[], int n, int start)
{
int dp[n-start];
dp[0] = 0;

for(int i = 1; i < n - start; i++)
{
dp[i] = 10000000;
for(int j = 0; j < i; j++)
{
if(a[j + start] >= (i-j))
{
dp[i] = min(dp[i], dp[j] + 1);
}
}
}

print(dp, n - start);

return dp[n-start-1];
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Using BFS :

``````private static int BFS(Queue<Node> q) {
while(!q.isEmpty()) {
Node node = q.remove();
int curInd = node.getIndex();
int curJump = node.getJump();

if(curInd == N)
return curJump;

int value = node.getValue();

for(int i = 1; i <= value; ++i) {
int index = curInd+i;
int jump = curJump+1;

if(isSafe(nodes, index)) {
nodes[index].setColor(COLOR.GREY);
nodes[index].setJump(jump);
nodes[index].setParent(curInd);

q.add(nodes[index]);
}
}

node.setColor(COLOR.BLACK);
}

return -1;
}

private static int getMinJump(Node[] nodes, int start, int end) {
Queue<Node> q = new LinkedList<Node>();

nodes[start].setColor(COLOR.GREY);
nodes[start].setJump(0);
nodes[start].setParent(-1);

q.add(nodes[start]);

return BFS(q);
}

private static boolean isSafe(Node[] nodes, int index) {
return index >= 1 && index <= N && nodes[index].getColor() == COLOR.WHITE;
}``````

Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Learn More

### 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.

Learn More

### 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.

Learn More

### 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.

Learn More