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)}`);
});
Sample input and output:
- Priyanka September 14, 2020Input: (hi)
(
hi
)
Input: (hi hello)
(
hi
hello
)
Input: (hi hello (bye))
(
hi
hello
(
bye
)
)