## Twitter Interview Question for Software Engineers

Country: United States
Interview Type: Phone Interview

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

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

Solution:
How to combine the 4 given card ranks with operators +, -, x and / to reach 24.
Backtrack to print all possible combinations.

``````public void solve(int[] cards) {
boolean[] played = new boolean[4];
for(int i = 0; i < 4; i++) {
StringBuilder str = new StringBuilder(String.valueOf(cards[i]));
played[i] = true;
solve(cards, played, cards[i],str);
played[i] = false;
}
}

private void solve(int[] cards, boolean[] played, double res, StringBuilder str) {
if(played[0] && played[1] && played[2] && played[3]) {
if(res == 24)
System.out.println(str.toString());
return;
}
for(int i = 0; i < 4; i++) {
int len = str.length();
if(!played[i]) {
played[i] = true;

str.insert(0, '(');
str.append("+" + cards[i] + ")");
solve(cards, played, res + cards[i], str);

str.setCharAt(len + 1, '-');
solve(cards, played, res - cards[i], str);

str.deleteCharAt(0);
str.deleteCharAt(str.length() - 1);
str.setCharAt(len, '*');
solve(cards, played, res * cards[i], str);

str.setCharAt(len, '/');
solve(cards, played, 1.0 * res / cards[i], str);

str.delete(len, str.length());
played[i] = false;
}
}
}``````

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.