Fortinet Interview Question for Software Engineer / Developers


Country: Canada
Interview Type: Written Test




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

Here N=9

int isSafe(int (*maze)[N],int x,int y)
{
	if(x>=0 && x<N && y>=0 && y<N && !maze[x][y])
		return 1;
	return 0;
}

void Maze(int (*maze)[N],int x,int y,int (*sol)[N])
{
	static int i,j;
	if(x==N-1 && y==N-1)
	{
		printf("\n\nSolution\n\n");
		sol[x][y]=1;
		for(i=0;i<N;i++)
		{
			for(j=0;j<N;j++)
				printf("%d ",sol[i][j]);
			printf("\n");
		}
	}
	else
	{
		if(isSafe(maze,x,y))
		{
			sol[x][y]=1;

			Maze(maze,x+1,y,sol);
			Maze(maze,x,y+1,sol);

			sol[x][y]=0;
		}
	}
}

- Aashish June 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

simply wrong, your mouse can only move downward or right.
This problem should be solved by BFS or A*.

- Tigre June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be very easily solved using backtracking. The solution given above is correct and it is basically using recursion and backtracking. To watch the complete solution of this problem using backtracking you can watch wonderful video at
youtu.be/keb6rP07Yqg

- coolsolution June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about more cleaner implementation?

- KillerCoder June 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

After move to (x, y), it's not enough to only try (x+1,y) and (x, y+1).
I think should try all the other 3 directions except the one that just come from.

- sandbox2006 September 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

pst, if the function maze(x,y) calls maze(x+1,y), then maze(x+1,y) is also gonna call maze(x+1+1,y),or maze(x+1,y+1), the terminology of this method is called recursion.

- Anonymous January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How to pass parameters to the function...can body right the code for the main?

- Ujjwal roy August 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdbool.h>

int n=5;
int M[5][5]={{1,1,0,1,0},{1,0,1,0,1},{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,0}};
int sol[5][5] = {0};
bool solve(int M[n][n], int x, int y, int sol[n][n]);
bool issafe (int M[n][n], int i, int j)
{
if (i >= 0 && i < n && j >= 0 && j <n && M[i][j] == 1)
return 1;
return 0;
}

bool Mazepath(int M[n][n])
{
if(!solve(M, 0,0,sol))
return false;
print(sol);
return true;
}

void print (int sol[n][n])
{
for(int i =0; i< n; i++) {
for(int j=0; j< n; j++) {
printf("%d\n", sol[i][j]);
}}
}

bool solve(int M[n][n], int x, int y, int sol[n][n])
{
if (x == n-1 && y ==n-1){
sol[x][y] =1; return true;
}
if(issafe(M, x,y)==true){
sol[x][y]=1;

if(solve(M,x+1,y,sol)) ;return true;
if(solve(M,x,y+1,sol)) ;return true;
//if(solve(M,x-1,y,sol)) return true;
//if(solve(M,x,y-1,sol)) return true;
sol[x][y]=0;
return false;
}
return true;
}

int main()
{
    Mazepath(M);
    return 0;
}

- Anonymous July 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdbool.h>

int n=5;
int M[5][5]={{1,1,0,1,0},{1,0,1,0,1},{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,0}};
int sol[5][5] = {0};
bool solve(int M[n][n], int x, int y, int sol[n][n]);
bool issafe (int M[n][n], int i, int j)
{
if (i >= 0 && i < n && j >= 0 && j <n && M[i][j] == 1)
return 1;
return 0;
}

bool Mazepath(int M[n][n])
{
if(!solve(M, 0,0,sol))
return false;
print(sol);
return true;
}

void print (int sol[n][n])
{
for(int i =0; i< n; i++) {
for(int j=0; j< n; j++) {
printf("%d\n", sol[i][j]);
}}
}

bool solve(int M[n][n], int x, int y, int sol[n][n])
{
if (x == n-1 && y ==n-1){
sol[x][y] =1; return true;
}
if(issafe(M, x,y)==true){
sol[x][y]=1;

if(solve(M,x+1,y,sol)) ;return true;
if(solve(M,x,y+1,sol)) ;return true;
//if(solve(M,x-1,y,sol)) return true;
//if(solve(M,x,y-1,sol)) return true;
sol[x][y]=0;
return false;
}
return true;
}

int main()
{
    Mazepath(M);
    return 0;
}

- Kamal July 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdbool.h>

int n=5;
int M[5][5]={{1,1,0,1,0},{1,0,1,0,1},{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,0}};
int sol[5][5] = {0};
bool solve(int M[n][n], int x, int y, int sol[n][n]);
bool issafe (int M[n][n], int i, int j)
{
if (i >= 0 && i < n && j >= 0 && j <n && M[i][j] == 1)
return 1;
return 0;
}

bool Mazepath(int M[n][n])
{
if(!solve(M, 0,0,sol))
return false;
print(sol);
return true;
}

void print (int sol[n][n])
{
for(int i =0; i< n; i++) {
for(int j=0; j< n; j++) {
printf("%d\n", sol[i][j]);
}}
}

bool solve(int M[n][n], int x, int y, int sol[n][n])
{
if (x == n-1 && y ==n-1){
sol[x][y] =1; return true;
}
if(issafe(M, x,y)==true){
sol[x][y]=1;

if(solve(M,x+1,y,sol)) ;return true;
if(solve(M,x,y+1,sol)) ;return true;
//if(solve(M,x-1,y,sol)) return true;
//if(solve(M,x,y-1,sol)) return true;
sol[x][y]=0;
return false;
}
return true;
}

int main()
{
    Mazepath(M);
    return 0;
}

- Kamal July 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
Maze(M, 0,0,sol)
}

- kamal July 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
#define RESULT_ROW 1024
#define N 5
bool back_tracking(int M[N][N],int x,int y,int **result,int *result_index){
////终止条件
if (x == N - 1 && y == N - 1 && M[x][y] == 0){
result[*result_index][0]= x;
result[*result_index][1] = y;
result_index++;
return true;
}

result[*result_index][0]= x;
result[*result_index][1] = y;
(*result_index)++;

if (x> N-1 || y> N-1) return false;
if (M[x][y] == 1){
return false;
}
if(back_tracking(M, x+1,y,result,result_index) == false){
(*result_index)--;
if (back_tracking(M, x,y+1,result,result_index) == false){
(*result_index)--;
return false;
}
}
return true;
}

bool find_one_path(int M[N][N],int x,int y){
int **result = (int **)malloc(sizeof(int*)*RESULT_ROW);
int result_count = 0;
for (int i = 0 ; i < RESULT_ROW;i++)
result[i]= (int *)malloc(sizeof(int)*2);
bool ret = back_tracking(M,0,0,result,&result_count);
if (ret){
printf("We find the path to the destination\n");
for(int i = 0 ;i < result_count -1 ;i++){
printf("(%d %d) ->",result[i][0],result[i][1]);
}
printf("(%d %d)",result[result_count -2][0],result[result_count -2][1]);
}
else
{
printf("We can not find the path to the destination\n");
}

for (int i = 0 ; i < RESULT_ROW;i++)
free(result[i]);
free(result);
}
void main(){
int M[N][N]={{0,0,0,1,0},{1,0,1,0,1},{1,0,0,0,0},{1,0,1,0,1},{0,0,1,0,0}};
return (find_one_path(M,0,0));
}


}

- wuchaoliang1980 September 24, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define RESULT_ROW 1024
#define N 5
bool back_tracking(int M[N][N],int x,int y,int **result,int *result_index){
////终止条件
if (x == N - 1 && y == N - 1 && M[x][y] == 0){
result[*result_index][0]= x;
result[*result_index][1] = y;
result_index++;
return true;
}

result[*result_index][0]= x;
result[*result_index][1] = y;
(*result_index)++;

if (x> N-1 || y> N-1) return false;
if (M[x][y] == 1){
return false;
}
if(back_tracking(M, x+1,y,result,result_index) == false){
(*result_index)--;
if (back_tracking(M, x,y+1,result,result_index) == false){
(*result_index)--;
return false;
}
}
return true;
}

bool find_one_path(int M[N][N],int x,int y){
int **result = (int **)malloc(sizeof(int*)*RESULT_ROW);
int result_count = 0;
for (int i = 0 ; i < RESULT_ROW;i++)
result[i]= (int *)malloc(sizeof(int)*2);
bool ret = back_tracking(M,0,0,result,&result_count);
if (ret){
printf("We find the path to the destination\n");
for(int i = 0 ;i < result_count -1 ;i++){
printf("(%d %d) ->",result[i][0],result[i][1]);
}
printf("(%d %d)",result[result_count -2][0],result[result_count -2][1]);
}
else
{
printf("We can not find the path to the destination\n");
}

for (int i = 0 ; i < RESULT_ROW;i++)
free(result[i]);
free(result);
}
void main(){
int M[N][N]={{0,0,0,1,0},{1,0,1,0,1},{1,0,0,0,0},{1,0,1,0,1},{0,0,1,0,0}};
return (find_one_path(M,0,0));
}

- wuchaoliang1980 September 24, 2021 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

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