Snap Inc Interview Question
Software DevelopersCountry: United States
Interview Type: Phone Interview
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");
}
}
}
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");
}
}
}
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");
}
}
}
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)}`);
});
// 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");
}
}
Sample input and output:
- Priyanka September 14, 2020Input: (hi)
(
hi
)
Input: (hi hello)
(
hi
hello
)
Input: (hi hello (bye))
(
hi
hello
(
bye
)
)