Adobe Interview Question for SDE-2s

Country: United States

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

``````public static int calcString(String str){

int sum =0;

Pattern p = Pattern.compile("[+-]?\\d+");
Matcher m = p.matcher(str);

while (m.find()) {
sum += Integer.parseInt(m.group().trim());
}
return sum;
}``````

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

In Java, I would something like:

``````public static int sum(String expression)
{
int sum = 0;
if(expression.length() == 0)
return 0;

for(int i = 0; i < expression.length() - 1; i = i + 2)
{
int value = Character.getNumericValue(expression.charAt(i));
char operator = expression.charAt(i + 1);
if(operator.equals("+"))
sum = sum + value;
else
sum = sum - value;

return sum;
}``````

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

It actually took me a while to finally go with the following solution:

``````public static int calcString(String str){

int sum =0;

Pattern p = Pattern.compile("[+-]?\\d+");
Matcher m = p.matcher(str);

while (m.find()) {
sum += Integer.parseInt(m.group().trim());
}
return sum;
}``````

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

``````int strsum(const char* str) {
int sum = 0;
int num = 0;
int sign = '+';
int c;
while(c = *str++) {
switch(c) {
case '+':
case '-':
sum = (sign == '+'? sum + num: sum - num);
sign = c;
num = 0;
break;
default:
num = num * 10 + (c - '0');
}
}
return (sign == '+'? sum + num: sum - num);
}``````

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

``````int strsum(const char* str) {
int sum = 0;
int num = 0;
int sign = '+';
int c;
while(c = *str++) {
switch(c) {
case '+':
case '-':
sum = (sign == '+'? sum + num: sum - num);
sign = c;
num = 0;
break;
default:
num = num * 10 + (c - '0');
}
}
return (sign == '+'? sum + num: sum - num);
}``````

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

``````int strsum(const char* str) {
int sum = 0;
int num = 0;
int sign = '+';
int c;
while(c = *str++) {
switch(c) {
case '+':
case '-':
sum = (sign == '+'? sum + num: sum - num);
sign = c;
num = 0;
break;
default:
num = num * 10 + (c - '0');
}
}
return (sign == '+'? sum + num: sum - num);
}``````

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

``````int strsum(const char* str) {
int sum = 0;
int num = 0;
int sign = '+';
int c;
while(c = *str++) {
switch(c) {
case '+':
case '-':
sum = (sign == '+'? sum + num: sum - num);
sign = c;
num = 0;
break;
default:
num = num * 10 + (c - '0');
}
}
return (sign == '+'? sum + num: sum - num);
}``````

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.