Questions d'entretien

Entretien pour Software Engineer Intern

-Menlo Park, CA

Meta

Given two strings representing integer numbers ("123" , "30") return a string representing the sum of the two numbers ("153")

Répondre

Réponses aux questions d'entretien

14 réponse(s)

5

public static String sumStrings(String a, String b){ char[] num1 = a.toCharArray(); char[] num2 = b.toCharArray(); int i = num1.length - 1; int j = num2.length - 1; StringBuilder sumString = new StringBuilder(); int carry = 0; while(i >= 0 || j >= 0){ int d1 = 0; int d2 = 0; if (i >= 0) d1 = num1[i--] - '0'; if (j >= 0) d2 = num2[j--] - '0'; int sum = d1 + d2 + carry; if (sum >= 10){ carry = sum / 10; sum = sum % 10; }else carry = 0; sumString.insert(0, sum); } return sumString.toString(); }

Luca Lupo le

3

The interviewer wanted a loop through the digits starting form right to left, adding them one by one, and keeping track of the carriage.

Utilisateur anonyme le

1

public static String addTwoStrings(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length(); char[] s1Chars = s1.toCharArray(); char[] s2Chars = s2.toCharArray(); StringBuilder sb = new StringBuilder(); int pointerA = len1 -1; int pointerB = len2 -1; int carry = 0; while (pointerA >= 0 || pointerB >= 0){ int a = pointerA 10) { carry = sumTemp / 10; sumTemp = sumTemp % 10; } else { carry = 0; } sb.insert(0, sumTemp); } if(carry >0) sb.insert(0, carry); return sb.toString(); }

Adding Two Strings which are valid numbers. Without converting it to Integers using internal method le

4

It's not stupid question, but it's not hard either. I believe the way to do it is to implement the manual addition process by looping through the digits starting from the right to left and adding them one by one. This is an O(N) operation. I'm not sure if there is a better way to do it.

Utilisateur anonyme le

3

It's not stupid a stupid question. What if the strings have 10000 characters?

Utilisateur anonyme le

0

public class StringToInt { public int stringToInt(String str) { int tens = 1; int num = 0; for(int i = 0; i < str.length(); ++i) { num += (str.charAt(str.length() - 1 - i) - '0') * tens; tens *= 10; } return num; } public int addStrings(String str1, String str2) { return stringToInt(str1) + stringToInt(str2); } public static void main(String [] args) { StringToInt s = new StringToInt(); System.out.println(s.addStrings("145", "23")); } }

Conner le

0

@Conner What if the strings are 1000 characters long? does your int tens and int num variables support that?

Gabriel le

0

int stringToNumber(char *a){ char *end = a; int it = 1; int acum = 0; while (*end != NULL){ end++; //move pointer to last char of string } while (&end != &a){ acum+=((*end - '0') * it); it *= 10; end--; } return acum; } int sum (char *a, char *b){ return stringToNumber(a) + stringToNumber(b); }

@staticaza le

0

import java.util.Arrays; import java.util.Scanner; public class AddNumericStrings { public static void main(String[] args) { final Scanner in = new Scanner(System.in); while (true) { System.out.println("Enter 2 numeric strings : "); String x = in.nextLine(); String y = in.nextLine(); System.out.println(add(x.toCharArray(), y.toCharArray())); } } private static char[] add(char[] big, char[] small) { char[] result = new char[big.length + 1]; Arrays.fill(result, '0'); for (int i = big.length - 1, j = small.length - 1; i >= 0 || j >= 0; i--, j--) { char x = big[i]; char y = '0'; if (j >= 0) { y = small[j]; } int val = x - '0'; val += (y - '0'); result[i+1] += val % 10; if (val > 10) { result[i] += (val/10); } } return result; } }

Ker le

0

You all know that negative integers exist, right? The question does not specify if the integers are non-negative. One just assume, therefore, that negative integers are possible. It would not be called subtraction. Subtraction does not exist. It would just be addition of the additive inverse.

Utilisateur anonyme le

0

Index the tuple to find the 1st & 2nd element. Convert the elements into ints and then convert the answer back to a string. Code: str(int(inp[0]) + int(inp[1]))

Utilisateur anonyme le

0

It is basic but yet not stupid. I assume that the interviewer asked to implement atoi and itoa (in case the interview was in C/C++).

Utilisateur anonyme le

0

lol it is a stupid question i agree. All you have to do is parse the strings add em parse em again and return em

Utilisateur anonyme le

3

I don't understand...it's a very stupid question! return Integer.toString(Integer.parseInt("123") + Integer.parseInt("30));

Matteo Gobbi le

Ajouter des réponses ou des commentaires

Pour commenter ceci, connectez-vous ou inscrivez-vous.