Goldman Sachs Interview Question
Software DevelopersCountry: India
Interview Type: Phone Interview
test1 = "ULDLLUDL"
test2 = "UP 2xDOWN LEFT 4xRIGHT"
def get_moves(s):
s = s.split(' ')
if len(s) > 1:
return s
else:
return list(s.pop())
def move_robo(x, y, m):
move = m.split('x')
k = 1
if len(move) == 2:
k = int(move.pop(0))
d = move.pop()[0]
if d == 'U':
y += (1 * k)
elif d == 'D':
y -= (1 * k)
elif d == 'L':
x -= (1 * k)
else:
x += (1 * k)
return x, y
def run_robo(s):
moves = get_moves(s)
x, y = 0, 0
for move in moves:
x, y = move_robo(x, y, move)
return x, y
print(run_robo(test1))
print(run_robo(test2))
const str = "ULDLLUDL";
const positionName = {
U : 'UP',
D : 'Down',
L : 'LEFT'
};
const postionCounter = new Map();
str.split('').forEach((val, index) => {
if(postionCounter.has(val)){
postionCounter.set(val, postionCounter.get(val) + 1);
}else{
postionCounter.set(val, 1);
}
});
let output = '';
postionCounter.forEach((val, index) => {
output += `${val}X${positionName[index]} `;
});
console.log(output);
const str = "ULDLLUDL";
const positionName = {
U : 'UP',
D : 'Down',
L : 'LEFT'
};
const postionCounter = new Map();
str.split('').forEach((val, index) => {
if(postionCounter.has(val)){
postionCounter.set(val, postionCounter.get(val) + 1);
}else{
postionCounter.set(val, 1);
}
});
let output = '';
postionCounter.forEach((val, index) => {
output += `${val}X${positionName[index]} `;
});
console.log(output);
const str = "ULDLLUDL";
const positionName = {
U : 'UP',
D : 'Down',
L : 'LEFT'
};
const postionCounter = new Map();
str.split('').forEach((val, index) => {
if(postionCounter.has(val)){
postionCounter.set(val, postionCounter.get(val) + 1);
}else{
postionCounter.set(val, 1);
}
});
let output = '';
postionCounter.forEach((val, index) => {
output += `${val}X${positionName[index]} `;
});
console.log(output);
- Anonymous January 10, 2018