Interview Question

Country: India

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

``````/*
Solution:
- looks like from hackerrank/topcoder, sure it was an interview?
- if you are at position (x,y) you can go either
to (x+1,y) or (x,y+1) both has an assosiated cost depending on
the direction you will be looking (0,1) where 0 means facing down
and 1 facing to the right:
- if you come from the left field, you will look right and only cost
is move cost
- if you come from top you will look down only cost is move
cost
- if facing right is cheaper when coming from top +
turning, use this cost
- same from coming from left + turning for facing down
so, there are two cost per field, one is reaching it facing
down and the other is reaching it and facing right.
...
- The O(n*m) iterative solution is as follows:
- c(d,x,y) denotes the cost at field x,y facing d: (d=0: down, d=1:
right), 0 <= x < M, 0 <= y < N
for(int y = 0; y < M; y++)
for(int x = 0; x < N; x++)
int c0 = MAX;
int c1 = MAX;
if(x > 0)
{
c1 = c[1][y][x-1] + move_cost[y][x];
c0 = c1 + turn_cost[y][x];
}
if(y > 0)
{
c0 = min(c0, c[0][y-1][x] + move_cost[y][x])
c1 = min(c1, c0 + turn_cost[y][x])
}
if(x == 0 && y == 0)
{ // start case
c0 = 0;
c1 = 0;
}
c[0][y][x] = c0;
c[1][y][x] = c1;

return min(c[0][N-1][M-1], c[1][N-1][M-1]);

complete solution in c++11:
*/

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <cassert>
#include <limits>

using namespace std;

int findMinCostToTraverseWithTurn(const vector<vector<int>>& move_cost, const vector<vector<int>>& turn_cost)
{
assert(move_cost.size()  > 0 && turn_cost.size() == move_cost.size());
assert(move_cost[0].size()  > 0 && turn_cost[0].size() == move_cost[0].size());
int n = move_cost.size();
int m = move_cost[0].size();

vector<vector<vector<int>>> c(2,
vector<vector<int>>(n,
vector<int>(m, numeric_limits<int>::max())));

// start case
c[0][0][0] = 0;
c[1][0][0] = 0;

for(int y = 0; y < n; y++)
{
for(int x = 0; x < m; x++)
{
if(x > 0)
{
c[1][y][x] = c[1][y][x - 1] + move_cost[y][x];
c[0][y][x] = c[1][y][x] + turn_cost[y][x];
}
if(y > 0)
{
c[0][y][x] = min(c[0][y][x], c[0][y - 1][x] + move_cost[y][x]);
c[1][y][x] = min(c[1][y][x], c[0][y][x] + turn_cost[y][x]);
}
// cout << "(" << c[0][y][x] << "," << c[1][y][x] << ") ";
}
// cout << endl;
}
return min(c[0][n - 1][m - 1], c[1][n - 1][m - 1]);
}

int main()
{
vector<vector<int>> move_cost {
{0,1,2},
{1,2,1},
{2,1,2}
};
vector<vector<int>> turn_cost {
{0,2,3},
{1,0,1},
{0,0,1}
};
cout << findMinCostToTraverseWithTurn(move_cost, turn_cost);
}``````

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

``````#include<bits/stdc++.h>
using namespace std;
int arr[1005][1005];
int mem[1005][1005][2];
int turn[1005][1005];
int main()
{
freopen("out.txt","w",stdout);
freopen("in.txt","r",stdin);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>arr[i][j];

}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
int x;
cin>>turn[i][j];
}
turn[n-1][m-1]=0;
mem[0][0][0]=arr[0][0];
mem[0][0][1]=arr[0][0];
for(int i=1;i<m;i++)
{
mem[0][i][0]=arr[0][i]+mem[0][i-1][0];
mem[0][i][1]=arr[0][i]+mem[0][i-1][0]+turn[0][i];

}
for(int i=1;i<n;i++)
{
mem[i][0][0]=arr[i][0]+mem[i-1][0][1]+turn[i][0];
mem[i][0][1]=arr[i][0]+mem[i-1][0][1];
}
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
{
mem[i][j][0]=arr[i][j]+min(mem[i-1][j][1]+turn[i][j],mem[i][j-1][0]);
mem[i][j][1]=arr[i][j]+min(mem[i-1][j][1],mem[i][j-1][0]+turn[i][j]);
}
cout<<mem[n-1][m-1][1];
return 0;
}``````

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

@Rohit. any example inputs?

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

#include<bits/stdc++.h>
using namespace std;
int arr[1005][1005];
int mem[1005][1005][2];
int turn[1005][1005];
int main()
{
freopen("out.txt","w",stdout);
freopen("in.txt","r",stdin);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>arr[i][j];

}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
int x;
cin>>turn[i][j];
}
turn[n-1][m-1]=0;
mem[0][0][0]=arr[0][0];
mem[0][0][1]=arr[0][0];
for(int i=1;i<m;i++)
{
mem[0][i][0]=arr[0][i]+mem[0][i-1][0];
mem[0][i][1]=arr[0][i]+mem[0][i-1][0]+turn[0][i];

}
for(int i=1;i<n;i++)
{
mem[i][0][0]=arr[i][0]+mem[i-1][0][1]+turn[i][0];
mem[i][0][1]=arr[i][0]+mem[i-1][0][1];
}
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
{
mem[i][j][0]=arr[i][j]+min(mem[i-1][j][1]+turn[i][j],mem[i][j-1][0]);
mem[i][j][1]=arr[i][j]+min(mem[i-1][j][1],mem[i][j-1][0]+turn[i][j]);
}
cout<<mem[n-1][m-1][1];
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.