## Snap Inc Interview Question for Software Developers

Country: United States
Interview Type: Phone Interview

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

Sample input and output:
Input: (hi)

(
hi
)

Input: (hi hello)

(
hi
hello
)

Input: (hi hello (bye))

(
hi
hello
(
bye
)
)

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

Just did a quick one in Python:

``````str1 = "(hi hello (bye))"
#print (str1)

strx=""
for i in range(len(str1)):
if (str1[i] == "(") or (str1[i] == " "):
strx+=str1[i]+"\n"
elif (str1[i]== ")"):
strx+="\n"+str1[i]
else:
strx+=str1[i]

print (strx)``````

Output:

``````(
hi
hello
)``````

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

``````public static void sameIndentLevel (String toIndent) {

int openParenthesis = 0;
int closeParenthesis = 0;

for (int i=0; i<toIndent.length(); i++) {
if (toIndent.charAt(i) == '(')
openParenthesis ++;
else if (toIndent.charAt(i) == ')')
closeParenthesis ++;
}

if ((openParenthesis-closeParenthesis) != 0) {
System.out.println("Please provide a well formatted string");
return;
}

int indentationLevel = 0;
String currentString = "";

for (int i=0; i<toIndent.length(); i++) {
currentString = "";
if (toIndent.charAt(i) == '(') {
indentationLevel ++;
}
else if (toIndent.charAt(i) == ')') {
indentationLevel --;
}
else {
while (i<toIndent.length() && (toIndent.charAt(i) != '(' && toIndent.charAt(i) != ')')) {
currentString = currentString += (Character.toString(toIndent.charAt(i)));
i++;
}
i--;
for (int j=0; j<indentationLevel; j++)
System.out.print("\t");
System.out.print(currentString);
System.out.println("\n");
}
}

}``````

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

``````public static void sameIndentLevel (String toIndent) {

int openParenthesis = 0;
int closeParenthesis = 0;

for (int i=0; i<toIndent.length(); i++) {
if (toIndent.charAt(i) == '(')
openParenthesis ++;
else if (toIndent.charAt(i) == ')')
closeParenthesis ++;
}

if ((openParenthesis-closeParenthesis) != 0) {
System.out.println("Please provide a well formatted string");
return;
}

int indentationLevel = 0;
String currentString = "";

for (int i=0; i<toIndent.length(); i++) {
currentString = "";
if (toIndent.charAt(i) == '(') {
indentationLevel ++;
}
else if (toIndent.charAt(i) == ')') {
indentationLevel --;
}
else {
while (i<toIndent.length() && (toIndent.charAt(i) != '(' && toIndent.charAt(i) != ')')) {
currentString = currentString += (Character.toString(toIndent.charAt(i)));
i++;
}
i--;
for (int j=0; j<indentationLevel; j++)
System.out.print("\t");
System.out.print(currentString);
System.out.println("\n");
}
}

}``````

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

public static void sameIndentLevel (String toIndent) {

int openParenthesis = 0;
int closeParenthesis = 0;

for (int i=0; i<toIndent.length(); i++) {
if (toIndent.charAt(i) == '(')
openParenthesis ++;
else if (toIndent.charAt(i) == ')')
closeParenthesis ++;
}

if ((openParenthesis-closeParenthesis) != 0) {
System.out.println("Please provide a well formatted string");
return;
}

int indentationLevel = 0;
String currentString = "";

for (int i=0; i<toIndent.length(); i++) {
currentString = "";
if (toIndent.charAt(i) == '(') {
indentationLevel ++;
}
else if (toIndent.charAt(i) == ')') {
indentationLevel --;
}
else {
while (i<toIndent.length() && (toIndent.charAt(i) != '(' && toIndent.charAt(i) != ')')) {
currentString = currentString += (Character.toString(toIndent.charAt(i)));
i++;
}
i--;
for (int j=0; j<indentationLevel; j++)
System.out.print("\t");
System.out.print(currentString);
System.out.println("\n");
}
}

}

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

``````const results = [];
const finalResults = [];
const ipString = "This is (John Doe (Lin Dan), (Carolina) (Woe)) (Kadambi)";
const markStart = index => {
if (results.length === 0) {
results.push({s: index, e: null, l: 0});
} else {
const newPoint = {...results[results.length - 1]};
newPoint.l++;
newPoint.s = index;
results.push(newPoint);
}
}
const markEnd = index => {
const lastEntry = results.pop();
lastEntry.e = index;
finalResults.push(lastEntry);
}
const parseChar = (char, index) => {
if (char === "(") markStart(index);
else if (char === ")") markEnd(index);
}

ipString.split("").forEach(parseChar);
finalResults.sort((a, b) =>
a.l < b.l
?  -1
: a.l > b.l
? 1
: 0).map(entry => {
const spacer = [];
for (let i=0; i<Math.pow(4, entry.l); i++) spacer.push(" ");
console.log(`\${spacer.join("")}\${ipString.slice(entry.s, entry.e + 1)}`);
});``````

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

``````// java solution
/* output
(
hi
)
(
hi
hello
)
(
hi
(
hello
(
what
a
wonderful
life!
)
)
bye
)
*/
import java.util.*;

public class MyClass {
public static void main(String args[]) throws Exception {
String input1 = "(hi)";
printExpressionIndented(input1);

String input2 = "(hi hello)";
printExpressionIndented(input2);

String input3 = "(hi (hello (what a wonderful life!)) bye)";
printExpressionIndented(input3);

}

static void printLineWithIndent(String str, int indentLevel) {
for (int i = 0; i < indentLevel; i++) {
System.out.print('\t');
}
System.out.println(str);
}

static void printExpressionIndented(String input) throws Exception {
printExpressionIndented(input, 0, 0);
}

static int printExpressionIndented(String input, int i, int indentLevel) throws Exception {
char curChar = input.charAt(i);
if (curChar != '(') throw new Exception("expected opening paranthesis at index " + i);
printLineWithIndent("" + curChar, indentLevel);
indentLevel++;
StringBuilder sb = new StringBuilder();

for (int j = i + 1; j < input.length(); ) {
curChar = input.charAt(j);
if (curChar == ')') {
if (sb.length() > 0) {
printLineWithIndent(sb.toString(), indentLevel);
sb.setLength(0);
}
indentLevel--;
printLineWithIndent("" + curChar, indentLevel);
return j + 1;
} else if (curChar == ' ') {
if (sb.length() > 0) {
printLineWithIndent(sb.toString(), indentLevel);
sb.setLength(0);
}
j++;
} else if (curChar == '(') {
j = printExpressionIndented(input, j, indentLevel);
} else {
sb.append(curChar);
j++;
}
}

throw new Exception("mismatched paranthesis");
}
}``````

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.