Intuit MailChimp CDP Senior SWE

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2024-07-18Added By Anonymous
Source: Link
1// Don't need to handle these special control characters 2// Slash (/) \/ 3// Backspace \b 4// Form feed \f 5// New line \n 6// Carriage return\r 7// Horizontal tab \t 8 9 10// Input: 11// {"key1":"val1","key2":["item1",{"item2key":1.2}]} 12 13// Output: 14// { 15// "key1": "val1", 16// "key2": [ 17// "item1", 18// { 19// "item2key": 1.2 20// } 21// ] 22 23 24// } 25 26const input = "{\"key1\":\"val1\",\"key2\":[\"item1\",{\"item2key\":1.2}]}"; 27const input2 = "{\"key,1\":\"val1\",\"key2\":[\"item1\",{\"item2key\":1.2}]}"; 28 29const solution = (somestr) => { 30 let indentCount = 0; 31 32 let finalStr = ""; 33 34 let isOpen = false; 35 36 for(let i = 0; i < somestr.length; i++) { 37 const char = somestr[i]; 38 if(char === "{") { 39 finalStr += char; 40 finalStr += "\n"; 41 indentCount++; 42 finalStr += "\t".repeat(indentCount); 43 } 44 else if(char === "}") { 45 finalStr += "\n"; 46 indentCount--; 47 finalStr += "\t".repeat(indentCount); 48 finalStr += char; 49 } 50 else if (char === "[") { 51 finalStr += char; 52 finalStr += "\n"; 53 indentCount++; 54 finalStr += "\t".repeat(indentCount); 55 } 56 else if (char === "]") { 57 finalStr += "\n"; 58 indentCount--; 59 finalStr += "\t".repeat(indentCount); 60 finalStr += "]"; 61 } 62 else if (char === ",") { 63 finalStr += char; 64 finalStr += "\n" 65 finalStr += "\t".repeat(indentCount); 66 } 67 else { 68 if(char === '"') { 69 isOpen = true 70 } 71 finalStr += char; 72 i++; 73 while(isOpen) { 74 const newChar = somestr[i]; 75 76 finalStr += newChar; 77 78 if(newChar === '"') { 79 isOpen = false; 80 } 81 i++; 82 } 83 i--; 84 } 85 } 86 87 console.log(finalStr) 88} 89solution(input2)


I completed the question with extra time left over but still was denied - not sure why. but here is the question they asked. you get a string and you need to format it and console log the formatted string.

Comments