Epic Systems Interview Question
Developer Program EngineersCountry: United States
#include<stdio.h>
#include<math.h>
int y=0;
int a=1;
int distance=0;
int direction=1;
int total_steps=0;
int current_total=0;
void move(int x){
int remaining_part;
if(x<0){
for(int i=-1; i>=x;i--){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
distance=distance+ (-direction)*1;
}
else{
direction=(-1)*direction;
remaining_part=abs(x-i)+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
distance=distance+(-direction)*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
else{
for(int i=1; i<=x;i++){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
distance=distance+ direction*1;
}
else{
direction=(-1)*direction;
remaining_part=abs(x-i)+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
distance=distance+direction*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
}
int main(){
int x;
printf("Enter the values for X , Y and Total \n");
scanf("%d %d %d",&x,&y,&total_steps);
while(current_total<total_steps){
move(direction*a*x);
move((-direction)*(x+a));
a=a+1;
}
printf("The distance travelled is %d ",abs(distance));
}
#include<stdio.h>
#include<math.h>
int y=0;
int a=1;
int distance=0;
int direction=1;
int total_steps=0;
int current_total=0;
void move(int x){
int remaining_part;
if(x<0){
for(int i=-1; i>=x;i--){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
distance=distance+ (-direction)*1;
}
else{
direction=(-1)*direction;
remaining_part=abs(x-i)+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
distance=distance+(-direction)*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
else{
for(int i=1; i<=x;i++){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
distance=distance+ direction*1;
}
else{
direction=(-1)*direction;
remaining_part=abs(x-i)+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
distance=distance+direction*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
}
int main(){
int x;
printf("Enter the values for X , Y and Total \n");
scanf("%d %d %d",&x,&y,&total_steps);
while(current_total<total_steps){
move(direction*a*x);
move((-direction)*(x+a));
a=a+1;
}
printf("The distance travelled is %d ",abs(distance));
}
#include<stdio.h>
#include<math.h>
int y=0;
int a=1;
int distance=0;
int direction=1;
int total_steps=0;
int current_total=0;
void move(int x){
int remaining_part;
if(x<0){
for(int i=-1; i>=x;i--){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
distance=distance+ (-direction)*1;
}
else{
direction=(-1)*direction;
remaining_part=abs(x-i)+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
distance=distance+(-direction)*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
else{
for(int i=1; i<=x;i++){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
distance=distance+ direction*1;
}
else{
direction=(-1)*direction;
remaining_part=abs(x-i)+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
distance=distance+direction*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
}
int main(){
int x;
printf("Enter the values for X , Y and Total \n");
scanf("%d %d %d",&x,&y,&total_steps);
while(current_total<total_steps){
move(direction*a*x);
move((-direction)*(x+a));
a=a+1;
}
printf("The distance travelled is %d ",abs(distance));
}
#include<stdio.h>
#include<math.h>
int y=0;
int a=1;
int add=11;
int subtract=22;
int distance=0;
int direction=1;
int total_steps=0;
int current_total=0;
void move(int x,int movement){
int remaining_part;
int abs_x=abs(x);
for(int i=1; i<=abs_x;i++){
if((current_total+1)<=total_steps){
if((current_total%y !=0)||(current_total==0)){
current_total=current_total+1;
if(movement==subtract)
distance=distance+ (-direction)*1;
else if(movement==add)
distance=distance+ direction*1;
}
else{
direction=(-1)*direction;
remaining_part=abs_x-i+1;
if(current_total+remaining_part>total_steps){
remaining_part=total_steps-current_total;
}
if(movement==subtract)
distance=distance+ (-direction)*remaining_part;
else if (movement==add)
distance=distance+ (direction)*remaining_part;
current_total=current_total+remaining_part;
return;
}
}
else{
return;
}
}
}
int main(){
int x;
printf("Enter the values for X , Y and Total \n");
scanf("%d %d %d",&x,&y,&total_steps);
while(current_total<total_steps){
move(a*x,add);
move((x+a),subtract);
a=a+1;
}
printf("The distance travelled is %d ",abs(distance));
}
public class Glitch_robot_distance {
public static void main(String args[])
{
move(2,2,5);
}
static void move(int x, int y, int steps)
{
int distance=0;
boolean forward=true;
for( int i=1;i<=steps;i++)
{
if(forward)
distance+= (x + i) - (x * i);
else
distance-=(x+i)-(x*i);
if((i%y)==0)
forward=!forward;
}
System.out.println(distance);
}
}
public class Glitch_robot_distance {
public static void main(String args[])
{
move(2,2,5);
}
static void move(int x, int y, int steps)
{
int distance=0;
boolean forward=true;
for( int i=1;i<=steps;i++)
{
if(forward)
distance+= (x + i) - (x * i);
else
distance-=(x+i)-(x*i);
if((i%y)==0)
forward=!forward;
}
System.out.println(distance);
}
}
public class Glitch_robot_distance {
public static void main(String args[])
{
move(2,2,5);
}
static void move(int x, int y, int steps)
{
int distance=0;
boolean forward=true;
for( int i=1;i<=steps;i++)
{
if(forward)
distance+= (x + i) - (x * i);
else
distance-=(x+i)-(x*i);
if((i%y)==0)
forward=!forward;
}
System.out.println(distance);
}
}
public class Glitch_robot_distance {
public static void main(String args[])
{
move(2,2,5);
}
static void move(int x, int y, int steps)
{
int distance=0;
boolean forward=true;
for( int i=1;i<=steps;i++)
{
if(forward)
distance+= (x + i) - (x * i);
else
distance-=(x+i)-(x*i);
if((i%y)==0)
forward=!forward;
}
System.out.println(distance);
}
}
public class Glitch_robot_distance {
public static void main(String args[])
{
move(2,2,5);
}
static void move(int x, int y, int steps)
{
int distance=0;
boolean forward=true;
for( int i=1;i<=steps;i++)
{
if(forward)
distance+= (x + i) - (x * i);
else
distance-=(x+i)-(x*i);
if((i%y)==0)
forward=!forward;
}
System.out.println(distance);
}
}
import java.util.Scanner;
public class Glitch {
public static void main(String[] args){
//program specification asks thats you prompt for input
Scanner keyboard = new Scanner(System.in);
System.out.print("x = ");
int x = keyboard.nextInt();
System.out.print("y = ");
int y = keyboard.nextInt();
System.out.print("total steps = ");
int totalSteps = keyboard.nextInt();
//run the Glitch Robot
GlitchRobot glitch = new GlitchRobot(x,y,totalSteps);
glitch.run();
System.out.println(glitch);
}
private static class GlitchRobot{
int totalSteps;
int startLocation;
int curLocation;
int x;
int y;
int direction;//either 1 or -1
public GlitchRobot(int x, int y,int totalSteps){
this.totalSteps = totalSteps;
startLocation = 0;
curLocation = 0;
this.x = x;
this.y = y;
direction = 1;//starts in positive direction (out multiplier)
}
public void run(){
int stepCounter = 0;
int stepIterator = 0;
while(stepCounter<totalSteps){
curLocation += (x * (stepIterator+1)) * direction;
stepCounter += (x * (stepIterator+1));
if(stepCounter>=totalSteps){
//we could be past totals steps so break
break;
}
//if y divides stepCounter turn 180 degrees. Since there are two different types of steps we have to compute it twice
if( y % stepCounter == 0)
direction*=-1;
curLocation -= (x + (stepIterator+1)) * direction;
stepCounter += (x + (stepIterator+1));
if(y % stepCounter == 0)
direction*=-1;
stepIterator++;
}
}
@Override
public String toString(){
return "steps from starting point: "+Math.abs(curLocation-startLocation);
}
}
}
Example solution in MATLAB that counts each step:
C = input('Enter three integers: x, y, and total number of steps, as a three element array.\nExample: [3 10 56]\n');
x=C(:,1);
y=C(:,2);
totalsteps=C(:,3);
direction = 1; %changes when robot flips 180 (i.e. total steps is a multiple of y)
currsteps = 0; %counter for total steps
distance = 0; %counter for distance from origin
level = 0; %level of steps, (ie 2x (level 2), 3x (level 3), 4x)
while currsteps<totalsteps
level = level+1
c = 0; %resettable counter for steps
cdum = x*level; %dummy variable for absolute value of current 1x, 2x, etc step
while c<cdum
c=c+1;
currsteps = currsteps+1;
distance = distance+1*direction;
if rem(currsteps,y)==0
direction = direction.*-1
end
if currsteps==totalsteps break
end
end
c=0;
cdum = x+level; %absolute value of current 1+x, 2+x, etc step
if currsteps==totalsteps break
end
while c<cdum
c=c+1;
currsteps = currsteps+1;
distance = distance-1*direction;
if rem(currsteps,y)==0
direction = direction.*-1
end
if currsteps==totalsteps break
end
end
end
fprintf('Glitch the robot ends up %f steps away from his starting point.', distance
public class Main {
public static int x = 3;
public static int y = 400;
public static int totalSteps = 500;
public static void main(String[] args) {
Robot robot = new Robot();
int position = robot.go(x, y, totalSteps);
System.out.println(new StringBuilder("x = ").append(x).append("; y = ").append(y).append("; totalSteps = ")
.append(totalSteps).append("; Result: ").append(position).toString());
}
}
class Robot {
public Robot() {}
public int go(int x, int y, int totalSteps) {
if ( x < 0 || y < 0 ) {
throw new IllegalArgumentException("Robot cannot move negative number of steps, but at least one of input parameters is negative.");
}
if (totalSteps <= 0) {
return 0;
}
int currentPosition = 0;
int currentDirection = 1;
int totalStepsPassed = 0;
Movement[] movements = {new MovementEven(x), new MovementOdd(x)};
int i = 1;
while (totalStepsPassed < totalSteps) {
Movement movement = movements[i++ % movements.length];
movement.ready();
while(movement.isNextStep() && totalStepsPassed < totalSteps) {
currentPosition += movement.step() * currentDirection;
totalStepsPassed++;
if (totalStepsPassed == y) {
currentDirection *= -1;
}
}
}
return currentPosition;
}
}
abstract class Movement {
// It counts number of steps within one movement
protected int passedSteps;
// It counts number of movements
protected int counter;
protected int x;
Movement(int x) {
this.x = x;
this.counter = 0;
this.passedSteps = 0;
}
public void ready() {
passedSteps = 0;
counter++;
}
abstract public boolean isNextStep();
public int step() {
passedSteps++;
return 1;
}
}
class MovementOdd extends Movement {
MovementOdd(int x) {
super(x);
}
public boolean isNextStep() {
return passedSteps < counter * x;
}
}
class MovementEven extends Movement {
MovementEven(int x) {
super(x);
}
public boolean isNextStep() {
return passedSteps < counter + x;
}
public int step() {
// even movements goes in backward direction
return super.step() * -1;
}
}
public class Main {
public static int x = 3;
public static int y = 400;
public static int totalSteps = 500;
public static void main(String[] args) {
Robot robot = new Robot();
int position = robot.go(x, y, totalSteps);
System.out.println(new StringBuilder("x = ").append(x).append("; y = ").append(y).append("; totalSteps = ")
.append(totalSteps).append("; Result: ").append(position).toString());
}
}
class Robot {
public Robot() {}
public int go(int x, int y, int totalSteps) {
if ( x < 0 || y < 0 ) {
throw new IllegalArgumentException("Robot cannot move negative number of steps, but at least one of input parameters is negative.");
}
if (totalSteps <= 0) {
return 0;
}
int currentPosition = 0;
int currentDirection = 1;
int totalStepsPassed = 0;
Movement[] movements = {new MovementEven(x), new MovementOdd(x)};
int i = 1;
while (totalStepsPassed < totalSteps) {
Movement movement = movements[i++ % movements.length];
movement.ready();
while(movement.isNextStep() && totalStepsPassed < totalSteps) {
currentPosition += movement.step() * currentDirection;
totalStepsPassed++;
if (totalStepsPassed == y) {
currentDirection *= -1;
}
}
}
return currentPosition;
}
}
abstract class Movement {
// It counts number of steps within one movement
protected int passedSteps;
// It counts number of movements
protected int counter;
protected int x;
Movement(int x) {
this.x = x;
this.counter = 0;
this.passedSteps = 0;
}
public void ready() {
passedSteps = 0;
counter++;
}
abstract public boolean isNextStep();
public int step() {
passedSteps++;
return 1;
}
}
class MovementOdd extends Movement {
MovementOdd(int x) {
super(x);
}
public boolean isNextStep() {
return passedSteps < counter * x;
}
}
class MovementEven extends Movement {
MovementEven(int x) {
super(x);
}
public boolean isNextStep() {
return passedSteps < counter + x;
}
public int step() {
// even movements goes in backward direction
return super.step() * -1;
}
}
- s100banerjee May 01, 2015