Panda Guru LogoPanda
Guru

Solve this if amazon was your dream company

Round 1

Questions: image
image
image

Specific question not provided.

Follow-up Question

Can you correct my code or tell the logic or write down your code in the comments?

class Result { public static int fillMissingBrackets(String s) { int n = s.length(); int[] prefixRound = new int[n]; int[] prefixSquare = new int[n]; int[] prefixQuestion = new int[n]; int openRound = 0, openSquare = 0, question = 0; for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c == '(') openRound++; if (c == ')') openRound--; if (c == '[') openSquare++; if (c == ']') openSquare--; if (c == '?') question++; prefixRound[i] = openRound; prefixSquare[i] = openSquare; prefixQuestion[i] = question; } int[] suffixRound = new int[n]; int[] suffixSquare = new int[n]; int[] suffixQuestion = new int[n]; openRound = 0; openSquare = 0; question = 0; for (int i = n - 1; i >= 0; i--) { char c = s.charAt(i); if (c == '(') openRound++; if (c == ')') openRound--; if (c == '[') openSquare++; if (c == ']') openSquare--; if (c == '?') question++; suffixRound[i] = openRound; suffixSquare[i] = openSquare; suffixQuestion[i] = question; } int validSplits = 0; for (int i = 0; i < n - 1; i++) { if (canBeBalanced(prefixRound[i], prefixSquare[i], prefixQuestion[i]) && canBeBalanced(suffixRound[i + 1], suffixSquare[i + 1], suffixQuestion[i + 1])) { validSplits++; } } return validSplits; } private static boolean canBeBalanced(int round, int square, int question) { return Math.abs(round) <= question && Math.abs(square) <= question; } }
Candidate's Approach

This is my solution for the Amazon SDE OA, but I ended up passing only 6 out of 15 test cases. I changed a few logic parts too, but it failed again. I got so disappointed after the exam.

Interviewer's Feedback

No feedback provided.