Struggler
BAN USER- 0of 0 votes
AnswersGiven a robot and a Maze in a game. Following our few of the primitives:
- Struggler in United States
bool IsMazeExit();
bool Forward();
void RightTurn();
Constraints:
Every room in the maze is a square. Each room has 2 doors. Robot can call IsExitMaze() when in a room and identify the exit. Forward moves the Robot forward few steps and if it finds a wall it returns to its position. Right turn turns the robot right 90 degrees.
Write a method to find the exit| Report Duplicate | Flag | PURGE
Amazon Software Engineer in Test Algorithm
void FindMaxDiff(int[] arr, out indx1, out indx2)
{
int len = arr.Length;
int i,j, max, min;
min = arr[0];max = 0;
indx1=indx2=0;
for (i=0,j=1;j<len;i++,j++)
{
if(arr[i]<arr[j])
{
if(arr[i]<min&&indx1 < j)
{
min = arr[i];
indx1 = i;
}
if(arr[j]>max&&indx2 >= indx1 )
{
max = arr[j];
indx2 = j;
}
}
}
}
// Indx2>Indx1 may not be required
//A c# version
void FindMaxDiff(int[] arr, out indx1, out indx2)
{
int len = arr.Length;
int i,j, max, min;
min = arr[0];max = 0;
for (i=0,j=1;j<len;i++,j++)
{
if(arr[i]<arr[j])
{
if(arr[i]<min)
{
min = arr[i];
indx1 = i;
}
if(arr[j]>max)
{
max = arr[j];
indx2 = j;
}
}
}
}
j is always greater than i, since we do not check the conditions for max being i and the rest is finding the max and min simultaneously. 3 comparisons for every 2 values better than 4 comparisons. O(n)
Please let me know if there are errors here
Order of numbers are not restored but still need to understand the Q
- Struggler September 17, 2012