Interview Question for Students


Country: United States




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

Wrote this up in Javascript, please provide your feedback :

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();

- kcsarath June 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please provide java/c++ code

- Kishore Kumar June 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please provide java/c++ code

- Akshay Kumar June 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please provide java/c++ code

- Shahrukh Khan June 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please provide java/c++ code

- Sunil Shetty June 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- shashank June 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can someone please provide a Java code for this problem ?

- Sid June 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- kcsarath June 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- JoyZombie August 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please provide java/ c++ code

- Rahul Jain June 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please provide java/c ++ code

- Ashok Kumar June 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please provide java/c++ code

- Govinda June 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can u plz tell in java?

- vd June 28, 2015 | Flag Reply
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;
}

- Anonymous June 28, 2015 | 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