Robot
1 experiences · Other (1)
Is my solution for LC 2069 generally acceptable as a pass in real interviews?
Interview Experience
cpp #include <cstdlib> #include <vector> using namespace std; class Robot { vector<vector<int>> directions = { {0,1}, // up {0,-1}, // down {-1,0}, // left {1,0} // right }; // starting position is at origin facing east pair<int,int> curPos = {0,0}; pair<int,int> curDir = {directions[3][0], directions[3][1]}; vector<vector<int>> grid; pair<int,int> turnLeft(pair<int,int>& d){ return {-d.second, d.first}; } pair<int,int> turnRight(pair<int,int>& d){ return {d.second, -d.first}; } public: Robot(int width, int height) { grid = vector<vector<int>>(width, vector<int>(height, 0)); } void step(int num) { int R = grid.size(); int C = grid[0].size(); int nx, ny; while (num > 0){ nx = curPos.first + (curDir.first * 1), ny = curPos.second + (curDir.second * 1); while (nx < 0 || nx > R - 1 || ny < 0 || ny > C - 1){ curDir = this->turnLeft(curDir); nx = curPos.first + (curDir.first * 1), ny = curPos.second + (curDir.second * 1); } curPos.first = nx, curPos.second = ny; num--; } } vector<int> getPos() { return {curPos.first, curPos.second}; } string getDir() { if (curDir == make_pair(0, 1)) return "North"; if (curDir == make_pair(0, -1)) return "South"; if (curDir == make_pair(-1, 0)) return "West"; if (curDir == make_pair(1, 0)) return "East"; return "Unknown"; } }; /** * Your Robot object will be instantiated and called as such: * Robot* obj = new Robot(width, height); * obj->step(num); * vector<int> param_2 = obj->getPos(); * string param_3 = obj->getDir(); */ 119/142 passed. time limit exceeded in extreme cases like very large grids i looked into the non-TLE solutions and it contains so much obscure knowledge that i dont thihk this was worth it my question is if i am asked this question in real interviews -- early round, final round or bar raiser doesnt matter -- can i just go with my solution and move on?