Amazon Interview Question for Software Engineers


Team: Seattle
Country: United States
Interview Type: Phone Interview




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

public class Main {

	public static void main(String[] args) {
		String s = "11aa22bb33dd44S";
		System.out.println("The string sum is "+stringSum(s));
	}
	
	public static int stringSum(String s){
		int sum = 0, curNumber = 0;
		for(int i=0; i<s.length();i++){
			if(Character.isDigit(s.charAt(i))){
				curNumber = curNumber*10 + (s.charAt(i)-'0');
			}else{
				sum += curNumber;
				curNumber = 0;
			}
		}
		return sum+curNumber;
	}
}

- settyblue July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

// ZoomBA
s = '11aa22bb33dd44' 
r = lfold( s.toCharArray , ['', 0 ] ) ->{
  if ( $.o @ '0123456789' ){
    $.p.0 += $.o 
  }else{
    $.p.1 += int( $.p.0 , 0 )
    $.p.0 = ''
  } 
  $.p  
}
r.1 += int( r.0 , 0 )
println(r.1)

- NoOne October 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Enter the string");
		string str = Console.ReadLine();
		str = str + " ";
		int sum=0;
		string strSum=string.Empty;
		for(int i=0;i<str.Length;i++)
		{
			if((str[i] >='0') && (str[i] <= '9'))
			{
					strSum = strSum + str[i];
			}
			else
			{
				if(strSum.Length>0)
				{
					sum = sum + Convert.ToInt32(strSum);
					strSum=string.Empty;
				}
			}
		}
		
		Console.WriteLine("sum: " + sum);
		
	}
}

- Ragesh July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package main

import "fmt"

const (
	Zero = 48
	Nine = 57
)

func sumString(s string) int {
	sum, i := 0, 0
	for i < len(s) {
		found, moved := consume(s[i:])
		sum += found
		i += moved
	}
	return sum
}

func consume(s string) (int, int) {
	neg := false
	i := 0
	sum := 0
	if s[i] == '-' {
		neg = true
		i++
	}
	for {
		digit, val := isDigit(s[i])
		i++
		if digit {
			sum = sum*10 + val
		} else {
			break
		}
	}
	if neg {
		sum = -sum
	}
	return sum, i
}

func isDigit(b byte) (bool, int) {
	if b >= Zero && b <= Nine {
		return true, int(b - Zero)
	}
	return false, 0
}

func main() {
	fmt.Println(sumString("this is 1 hairy dog and 2 cats"))
	fmt.Println(sumString("this is 11 hairy dogs and 23 cats"))
	fmt.Println(sumString("what is -2 + 3?"))
	fmt.Println(sumString("1 cat and 10 chickens"))
	fmt.Println(sumString("what-you bought 12 pairs of shoes and 3 handbags?"))
}

- agm July 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int calsumofstring(string str) {
bool sign = false;
string currentstr = "";
int sum = 0;
for (int i = 0; i < str.size(); i++) {
char charvalue = str.at(i);
if (charvalue >= '0' && charvalue <= '9') {
sign = true;
currentstr += charvalue;
}
else {
sum += atoi(currentstr.c_str());
currentstr = "";
sign = false;
}

}
return sum;
}

- Jack888 July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String givenString = "11aa22bb33dd44";
		int sumOfNumber=0;
		int inc_next_integer=0;
        boolean flag =false;
		for(int i=0; i<givenString.length();i++)
		{
			if(Character.isDigit(givenString.charAt(i)))
			{
				inc_next_integer++;
				flag=true;
			}
			if(Character.isLetter(givenString.charAt(i)))
			{
				if(flag){
				String subString = givenString.substring(i-(inc_next_integer),i);
				inc_next_integer=0;
				sumOfNumber +=Integer.parseInt(subString);
				}
				flag=false;
			}
			else
			{
				if( i==givenString.length()-1)
					if(flag){
						String subString = givenString.substring(givenString.length()-inc_next_integer);
						sumOfNumber +=Integer.parseInt(subString);
					}
			}
			
		}
		System.out.println( "GivenString  "+givenString+" Total Value : "+sumOfNumber);

- Sathya July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String givenString = "11aa22bb33dd44";

- Sathya July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.my.example;

public class AddDigital {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String givenString = "11aa22bb33dd44";
		int sumOfNumber=0;
		int inc_next_integer=0;
        boolean flag =false;
		for(int i=0; i<givenString.length();i++)
		{
			if(Character.isDigit(givenString.charAt(i)))
			{
				inc_next_integer++;
				flag=true;
			}
			if(Character.isLetter(givenString.charAt(i)))
			{
				if(flag){
				String subString = givenString.substring(i-(inc_next_integer),i);
				inc_next_integer=0;
				sumOfNumber +=Integer.parseInt(subString);
				}
				flag=false;
			}
			else
			{
				if( i==givenString.length()-1)
					if(flag){
						String subString = givenString.substring(givenString.length()-inc_next_integer);
						sumOfNumber +=Integer.parseInt(subString);
					}
			}
			
		}
		System.out.println( "GivenString  "+givenString+" Total Value : "+sumOfNumber);
	}

}

- Sathya July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringSum {

public static void main(String[] args) {
String str = "11aa22bb33dd444";
int sum=0;
String revString = new StringBuilder(str).reverse().toString();
char[] charArray = revString.toCharArray();
int tens = 1;
for (int i = 0; i < charArray.length; i++) {
if (Character.isDigit(charArray[i])) {
int num = Character.getNumericValue(charArray[i])*tens;

sum=sum+num;
tens=tens*10;
}
else
{
tens=1;
}
}
System.out.println(sum);
}
}

- Anonymous July 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringSum {

	public static void main(String[] args) {
		String str = "11aa22bb33dd444";
		int sum=0;
		String revString = new StringBuilder(str).reverse().toString();
		char[] charArray = revString.toCharArray();
		int tens = 1;
		for (int i = 0; i < charArray.length; i++) {
			if (Character.isDigit(charArray[i])) {
				int num = Character.getNumericValue(charArray[i])*tens;
				
				sum=sum+num;
				tens=tens*10;
			}
			else
			{
				tens=1;
			}
		}
		System.out.println(sum);
	}
}

- Anonymous July 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringSum {

public static void main(String[] args) {
String str = "11aa22bb33dd444";
int sum=0;
String revString = new StringBuilder(str).reverse().toString();
char[] charArray = revString.toCharArray();
int tens = 1;
for (int i = 0; i < charArray.length; i++) {
if (Character.isDigit(charArray[i])) {
int num = Character.getNumericValue(charArray[i])*tens;

sum=sum+num;
tens=tens*10;
}
else
{
tens=1;
}
}
System.out.println(sum);
}
}

- tanveer July 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ public class StringSum { public static void main(String[] args) { String str = "11aa22bb33dd444"; int sum=0; String revString = new StringBuilder(str).reverse().toString(); char[] charArray = revString.toCharArray(); int tens = 1; for (int i = 0; i < charArray.length; i++) { if (Character.isDigit(charArray[i])) { int num = Character.getNumericValue(charArray[i])*tens; sum=sum+num; tens=tens*10; } else { tens=1; } } System.out.println(sum); } } }} - tanveer July 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CalculateSumOfIntegersInString {
	
	public static void main(String[] args) {
		String problem = "aa11aa22bb33dd44";
		
		problem += "_";
		
		boolean numberEncountered = false;
		int totalNumber = 0;
		String currentNumber = "";
		
		for(int i = 0; i < problem.length(); i++) {
			char charAt = problem.charAt(i);
			
			if(Character.isDigit(charAt)) { 
				currentNumber = currentNumber + charAt;
				numberEncountered = true;
			} else {
				if(numberEncountered) {
					numberEncountered = false;
					totalNumber += Integer.parseInt(currentNumber);
					currentNumber = "";
				}
			}
		}
		
		System.out.println(totalNumber);
	}

}

- cskamil July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package amazon;

public class SumOfAllNumInString {
    public static void main(String[] args) {
        String s = "11aa22b01b33dd44";
        int n =0,sum=0;
        for(char x :s.toCharArray()){
            if(x>=48 && x<=57){
                n = (n*10)+(x-48);
            }else{
                sum+=n;
                n =0;
            }
        }
        System.out.println(sum+n);
    }
}

- Abhinaw Bhagat July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>
#include <conio.h>

bool checkIsdigit(char* c)
{
if((c>=a) && (c<=z)) //or if(c>=0 && c<=9) return true else false;
return false;

return true;
}

int calculateSumFromString(string s)
{
int result=0; intermediateres=0;
for(int i=0;i<s.length(); i++)
{
if(checkIsdigit(s[i])
{
intermediateres=intermediateres*10+ (int) (s[i]); //or we can use atoi(s[i]): atoi converts string to integer
}
else
{
result=result + intermediateres;
intermediateres=0;
}
}
result=result + intermediateres;
return result;
}

int main()
{
return calculateSumFromString("11aa22bb33dd44");
}

- Poon August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function sumStr(str){

var resultArray = [], numSumArray = [];

resultArray = str.split("");

numSumArray = resultArray.filter(function(val){
return (parseInt(val));
});

return numSumArray.join("");

}

- Anonymous August 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class UidaiDataProcessor {

	public static void main(String args[]) {

		String num = "10aa20bb30dd40nnnnnnnnnnm10bbhbsdjkjwqkdn1lkjlklkmmcmndf9";
		String n = "";
		boolean inc = false;
		for (int i = 0; i < num.length(); i++) {
			if (Character.isDigit(num.charAt(i))) {
				n = n + num.charAt(i);
				inc = false;
			} else {
				if (!inc)
					n = n + "-";
				inc = true;
			}
		}
		String s[] = n.split("\\-");
		Integer k = 0;
		for (String a : s) {
			k = k + Integer.parseInt(a);
		}
		System.out.println(k);
}
}

- sree August 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <cstdlib>


using namespace std;

bool isDigit(char c)
{
	return c >= '0' && c <= '9';
}

int getSum(const std::string &str)
{
	int sum = 0;
	std::string tmpStr;
	int p1 = -1;
	int p2 = -1;
	for(int i = 0; i < str.size(); ++i)
	{
		if(isDigit(str[i]))
		{
			if(p1 == -1)
			{
				p1 = i;
			}
			else
			{
				p2 = i;
				tmpStr = str.substr(p1, p2 - p1 + 1);
				sum += atoi(tmpStr.c_str());
				p1 = -1;
			}
		}
	}
	return sum;
}

int main()
{
	cout<<getSum("11aa22bb33cc44dd")<<endl; //110
	cout<<getSum("1q2w3e4r5t6y7u8i9o0p")<<endl; //25

}

- cpp guy August 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sumTheNumbers(String s) {
		int sum = 0;
		if (s == null || s.isEmpty()) {
			return sum;
		}

		int currentValue = 0;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (Character.isDigit(c)) {
				if (currentValue > 0) {
					currentValue = currentValue * 10 + Character.getNumericValue(c);
				} else {
					currentValue = Character.getNumericValue(c);
				}
			} else {
				sum = sum + currentValue;
				currentValue = 0;
			}
		}
		return sum + currentValue;
	}

- kvdeshmukh1989 January 16, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More