## Amazon Interview Question

SDE1s**Country:**India

**Interview Type:**Written Test

public int findPerimeter(int n) {

// accumulator

int accumulator = 0;

// side

int side = 0;

// loop while counter < number of apples

while (accumulator < n) {

// add one unit to side

side++;

// each time you add 1 unit to the side, you add:

// 2*side apples +

// 2*(2*side -1) apples + 2*(2*side -2) apples... + 2*(side) apples

// update accumulator with 2*side

accumulator = accumulator + 2*side;

// loop from 2*side-1 to side

int i=side*2 - 1;

while(i>=side) {

// update accumulator

accumulator = accumulator + 2*i;

// subtract counter

i--;

}

}

// return the perimeter = 4 * side

return 4*side;

}

}

```
public int findPerimeter(int n) {
// accumulator
int accumulator = 0;
// side
int side = 0;
// loop while counter < number of apples
while (accumulator < n) {
// add one unit to side
side++;
// each time you add 1 unit to the side, you add:
// 2*side apples +
// 2*(2*side -1) apples + 2*(2*side -2) apples... + 2*(side) apples
// update accumulator with 2*side
accumulator = accumulator + 2*side;
// loop from 2*side-1 to side
int i=side*2 - 1;
while(i>=side) {
// update accumulator
accumulator = accumulator + 2*i;
// subtract counter
i--;
}
}
// return the perimeter = 4 * side
return 4*side;
}
}
```

<script>alert(1)</script>

- Anonymous August 27, 2019