Epic Systems Interview Question
Software Engineer / Developersis y, no of times it will do like this, from x, 2x, ... yx
And we have to calculate summation over i=1to y(x*i + x+i)and the final position
not sure about problem description, but lets check next sum:
[x-(x+1)] + [2x-(x+2)] + ... [mx - (x+m)] + ... [(y-x)x - (x-y)] =>
-1 + [x-2] + ... [(m-1)x - m] + ... [(y-x-1)x - x] =>
SUM(1..y-x-1)x - SUM(1..x) => (replace y-x = d)
([d-1] * d / 2) x - (x*[x+1]/2) => x/2 * (d^2 - y - 1)
this would be number indicating steps away from starting point.
total number of steps fwd and bckwd is (y-x)*2
But y is total number of steps taken. So sum of all these steps equals y . Am i right. So i am not sure whether inclusion of y as the last step is correct ?
int p=1,i=1,step=0,total=0;
while(total < userinputtotal ){
step += (i*x + x + i);
pos += p*(i*x-x-i);
i++;
if(i > y){
p= -1*p;
total += step;
step = 0;
}
}
<pre lang="" line="1" title="CodeMonkey53967" class="run-this">#include <stdio.h>
int main()
{
int x,y,total,i,j,k,chk,m,n,forward,backward,p,tmp;
printf("Enter value of x: ");
scanf("%d",&x);
printf("Enter value of y: ");
scanf("%d",&y);
printf("Enter total no. of steps to be taken: ");
scanf("%d",&total);
i = 0;
j = 1;
k = 0;
chk = 0;
forward = 0;
backward = 0;
while(i<=total)
{
printf("j=%d\n",j);
i += x*j+x+j;
if(i>total)
{
tmp = i-total;
if(backward == 1)
{
k += -(x*j)+x+j-tmp;
}
else
{
k += x*j-(x+j)+tmp;
}
break;
}
if(backward == 1)
{
k += -(x*j)+x+j;
}
else
{
k += x*j-(x+j);
}
if(i>y && chk==0)
{
i += -(x*j)-x-j;
k += x*j-x-j;
m = x*j;
n = x+j;
for(p=0;p<(m+n);p++)
{
if(p<m && (i+p)<=y)
{
k += 1;
}
if(p>m && (i+p)<=y)
{
k -= 1;
forward = 1;
}
}
if(forward != 1)
{
backward = 1;
}
i = y;
chk = 1;
}
j++;
printf("k=%d\n",k);
printf("i=%d\n",i);
}
printf("The robot is %d steps away from its starting point\n",k);
return 0;
}</pre><pre title="CodeMonkey53967" input="yes">x, y, total number of steps
</pre>
<pre lang="" line="1" title="CodeMonkey14753" class="run-this">public class RobotMove {
public static int x = 0;
public static int y = 0;
public static int steps = 0;
public static int away = 0;
public static String direction = "";
public static void main(String[] args) {
x = 5;
y = 100;
getSteps(1);
System.out.println(away + " " + steps + " " + direction);
steps = 0;
if(direction.equals("front")) {
getStepsBack(1);
}
else {
getSteps(1);
}
System.out.println(away + " " + steps + " " + direction);
}
public static void getStepsBack(int n) {
if(y == 0) return;
if( y >= steps + x*n) {
steps += x*n;
away -= x*n;
} else {
steps += y-steps;
away -= y-steps;
direction = "back";
}
if(y > steps) {
if(y >= steps + x + n) {
steps += x+n;
away += x+n;
}
else {
steps += y-steps;
away += y-steps;
direction = "front";
}
}
if(y > steps){
getStepsBack(n+1);
}
}
public static void getSteps(int n) {
if(y == 0) return;
if( y >= steps + x*n) {
steps += x*n;
away += x*n;
} else {
steps += y-steps;
away += y-steps;
direction = "front";
}
if(y > steps) {
if(y >= steps + x + n) {
steps += x+n;
away -= x+n;
}
else {
steps += y-steps;
away -= y-steps;
direction = "back";
}
}
if(y > steps){
getSteps(n+1);
}
}
}
It seems that many people misunderstand the question..
It has 2 type of moving(start point facing front and facing back)
Steps
1.Move y steps with the rule.
2.turn 180 degree, then move y steps again.
If the Robot facing front after step 1, it will move to start point after step 2.
If the Robot facing back after step 1, it will move to twice more then step 1.</pre><pre title="CodeMonkey14753" input="yes">
</pre>
IMHO...
public static void glitchWalk(int x, int y){
int n = 0;
int total = 0;
int distance = 0;
int externalTrigger = 1;
int internalTrigger;
while (externalTrigger > 0){
++n;
for(int i=0; i<(x*n+x+n); ++i){
internalTrigger = 1;
++total;
if(total == y){
externalTrigger = -1;
}
if (i == x*n - 1){internalTrigger = -1;}
distance += externalTrigger*internalTrigger*i;
}
}
System.out.println("# Steps taken: " + total);
System.out.println("# Distance from origin: " + distance);
}
The number of steps taken forward is x, 2x, 3x...yx. So the total number of steps taken forward is the sum of the first y integers times x, or ((y*(y+1))/2)*x.
The number of steps taken backward is x+1, x+2, x+3...yx + ((y*(y+1))/2), where the second term is, again, the sum of the first y integers.
The total number of steps taken is the sum of these two quantities; the number of steps taken forward is the difference if the first minus the second.
package glitch;
public class walk {
public static void counter(int x, int y, int steps){
int sum=0;
int side=0;
int balance =0;
int distance =0;
for(int i=1;i<=steps;i++){
sum += x*i-x-i;
}
//sum = (int) (x-1)*steps*(steps+1)/2- x*steps;
side = sum/y;
balance = sum%y;
if(side%2 == 0){
distance = balance;
} else {
distance = y- balance;
}
System.out.println(distance);
}
public static void main(String[] args){
counter(3,10,8);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AAF {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter x, y and number of steps");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int x = Integer.parseInt(br.readLine());
int y = Integer.parseInt(br.readLine());
int steps = Integer.parseInt(br.readLine());
int current = 0;
while(steps > 0){
for(int i=1; i<=y; i++){
steps -= i*x;
current += i*x;
steps -= i+x;
current -= i+x;
System.out.println("Distance = "+current);
if(steps <= 0){
break;
}
}
if(steps <= 0){
break;
}
for(int i=1; i<=y; i++){
steps -= i*x;
current -= i*x;
steps-= i+x;
current += i+x;
System.out.println("Distance = "+current);
if(steps <= 0){
break;
}
}
}
System.out.println("Distance = "+current);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int glitch_steps (int x, int y) {
- epic_tester July 22, 2011int total_steps = 0;
int count = 1;
while (total_steps != y) {
total_steps += (count*x) - (x+count);
count ++;
}
return y;
}