## Interview Question for Students

Country: United States

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

``````var buildArray = function (length, breadth) {
var array = new Array();
for (var i = 0; i < length; i++) {
array[i] = [
];
}
return array;
}
var initialize = function (array) {
var count = 1;
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
array[i][j] = count++;
}
}
return array;
}
var displayArray = function (array) {
array.forEach(function (entry) {
console.log(entry);
});
}
var findPath = function (array, src, dest) {
console.log('finding path from src, ' + src + ' to destination ' + dest);
var srcCoord = getCoordinates(src);
var destCoord = getCoordinates(dest);

path = traverse(array, srcCoord, destCoord);
return path;
}

var traverse = function (array, srcCoord, destCoord ) {
var path = "";
var rSign = (destCoord.row > srcCoord.row) ? +1 : -1;

var r = srcCoord.row;
for ( ;  ;r += rSign) {
path += " " + array[r][srcCoord.col] + " ";
if (r == destCoord.row) break;
}

var cSign = (destCoord.col > srcCoord.col) ? +1 : -1;
var c = srcCoord.col + cSign;
for ( ; ; c += cSign) {
if (c == destCoord.col + cSign) {break;}
path += " " + array[destCoord.row][c] + " ";
}
return path;
}

var getCoordinates = function (value) {
var row = Math.floor((value - 1 )/ 5); // console.log ( "Row = " + row);
var rowStart = (row * 5) + 1;  // console.log ("Row Start = " + rowStart);
var col = value - rowStart;  // console.log ("Col = " + col);
return {
'row': row,
'col': col
};
}
var main = function () {
var array = buildArray(5, 5);
console.log(array);
array = initialize(array);
displayArray(array);
var endPoints = [
{
src: 1,
dest: 22
},
{ src: 18, dest: 7},
{
src: 4,
dest: 17
},
{
src: 5,
dest: 18
},
{
src: 9,
dest: 13
},
{
src: 20,
dest: 23
}
];

endPoints.forEach(function (entry) {
var path = findPath(array, entry.src, entry.dest);
console.log ("The path is " + path);
});
}
main();``````

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

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

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

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

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

1. no output for: 1 16
2. shows overlapping coordinate for:
1 17
2 21

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

Can someone please provide a Java code for this problem ?

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

Thanks Shashank for pointing the issue with 1, 16. The traverse function needed the following fix.

``````for ( ; ; c += cSign) {
if (c == destCoord.col + cSign) {break;}
path += " " + array[destCoord.row][c] + " ";
}``````

I do not see a problem with 1, 17 and 2, 21 !!

Will post a java version in some time.

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

I think, this code doesn't take care of overlapping and covering entire grid. Moreover, I'm not able to find DP here.

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

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

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

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

can u plz tell in java?

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

wrote this in C++ if optimal answer is not required i guess this would work... tell me if i'm wrong...

#include <iostream>
using namespace std;
int mat[5][5]={{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};

int main() {
int x,y,row,col;
cin>>x;
if(1<=x&&x<=5){
row=0;
}else if(6<=x&&x<=10){
row=1;
}else if(11<=x&&x<=15){
row=2;
}else if(16<=x&&x<=20){
row=3;
}else if(21<=x&&x<=25){
row=4;
}
cin>>y;
if(1<=y && y<=5){
col=0;
}else if(6<=y && y<=10){
col=1;
}else if(11<=y&&y<=15){
col=2;
}else if(16<=y&&y<=20){
col=3;
}else if(21<=y&&y<=25){
col=4;
}
//cout<<row<<" "<<col<<"\n";
if(row==col){
while(x<=y){
cout<<x<<" ";
x++;
}
}else if(row<col){
while(row<=col){
cout<<x<<" ";
x+=5;
row++;
}
x-=5;
if(x!=y){
if(x>y){
while(x>=y){
x--;
if(x<y)
break;
cout<<x<<" ";
}
}else{
while(x<=y){
x++;
if(x>y)
break;
cout<<x<<" ";
}
}
}
}

return 0;
}

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.

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