filiptod@uw.edu
BAN USER
Comments (3)
Reputation 10
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
My bad, rushed the typing of the code after writing it on paper:
void arrayProduct(int arr[]) {
int arrLength = arr.length();
if (arrLength > 1) {
for (int i = 0; i < arrLength; i++) {
int product = 1;
for (int j = 0; j < arrLength; j++) {
if (j != i) {
product *= arr[j];
}
}
arr[i] = product;
product = 1;
}
}
}
Comment hidden because of low score. Click to expand.
0
of 0 vote
O(n^2) but good starting ground before optimizing your code:
void arrayProduct(int arr[]) {
int arrLength = arr.length();
if (arrLength > 1) {
int product = 1;
for (int j = 0; j < arrLength ; j++) {
if (j != i) {
product *= arr[j];
}
}
arr[i] = product;
product = 1;
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
@eng.ahmed.moustafa: In my opinion, you should take advantage of the fact that an implementation using stacks can later easily have other operators and parentheses added with little/none of the original code changed. Short code isn't always the solution as upgrade-ability/modularity is a big factor and can land you some nice bonus points in an interview if you describe it well. Unless the interviewer specifically rules out the use of data structures, I'd say a stack implementation is probably best here.
- filiptod@uw.edu February 27, 2015EDIT: It's almost certain that after you finished writing your code, they will ask "Ok, now what if we want to add '/' and '-' functionality, what will you do?" so making your code as modifiable and scalable is always a plus.