## Amazon Interview Question for SDE1s

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

Approach-1:

``````public String pushDominoes(String dominoes) {
if(dominoes.length() == 0)
return dominoes;

char result[] = new char[dominoes.length()];
char lastDominoe = '.';
int lastIndex = 0;

for(int i = 0; i < dominoes.length(); i++){
char ch = dominoes.charAt(i);
result[i] = ch;

if(ch == '.'){
continue;
}

if(lastDominoe == '.'){
if(ch != 'R'){
int k = i - 1;
while(k >= lastIndex){
result[k] = ch;
k--;
}
}
}else{
if(lastDominoe == 'R' && ch == 'L'){
int low = lastIndex + 1;
int high = i - 1;

while(low < high){
result[low] = 'R';
result[high] = 'L';
low++;
high--;
}
}else if(lastDominoe == ch){
int low = lastIndex + 1;

while(low < i){
result[low] = ch;
low++;
}
}
}

lastDominoe = ch;
lastIndex = i;
}

if(lastDominoe == 'R' && lastIndex < dominoes.length()){
lastIndex++;
while(lastIndex < dominoes.length()){
result[lastIndex++] = 'R';
}
}

return String.valueOf(result);
}``````

Approach-2:

``````public String pushDominoes(String dominoes) {
int n = dominoes.length();
if(n == 0)
return dominoes;

int f[] = new int[n];
char A[] = dominoes.toCharArray();

int force = 0;

for(int i = 0; i < n; i++){
char ch = A[i];
if(ch == 'R')
force = n;
else if(ch == 'L')
force = 0;
else
force = Math.max(force - 1, 0);

f[i] += force;
}

force = 0;
for(int i = n - 1; i >= 0; i--){
char ch = A[i];
if(ch == 'L')
force = n;
else if(ch == 'R')
force = 0;
else
force = Math.max(force - 1, 0);

f[i] -= force;
}

StringBuilder result = new StringBuilder();

for(int i = 0; i < n; i++){
result.append(f[i] > 0 ? "R" : (f[i] < 0 ? "L" : "."));
}

return result.toString();
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.