Amazon Interview Question
Software Engineer in TestsUsing the above code with stack fails for the input [}{)(]
I have tried the classic way. Let me know, if you see any bugs.
public static bool isWellFormed(String input)
{
if (input == null) return true;
if (input.Length == 0) return true;
int count = 0;
for (int i = 0; i < input.Length; i++)
{
char iChar=input[i];
if (iChar == '(' || iChar == '{' || iChar == '[')
count++;
if (input[i] == ')' || iChar == '}' || iChar == ']')
count--;
}
return ((count==0)?true:false);
}
<pre lang="" line="1" title="CodeMonkey59664" class="run-this">import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class WellFormedString {
static Map<Character, Character> map = new HashMap<Character, Character>();
static {
map.put(']', '[');
map.put('}', '{');
map.put(')', '(');
}
public static void main(String[] args) {
System.out.println(isWellFormed("(}"));
}
public static boolean isWellFormed(String input) {
if (input == null)
return true;
if (input.length() == 0)
return true;
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < input.length(); ++i) {
char temp = input.charAt(i);
if ((temp == '[') || (temp == '{') || (temp == '('))
stack.push(temp);
if ((temp == ']') || (temp == '}') || (temp == ')')) {
if (stack.isEmpty())
return false;
if (map.get(temp) != stack.pop())
return false;
}
}
if (!stack.isEmpty())
return false;
return true;
}
}
</pre><pre title="CodeMonkey59664" input="yes">
</pre>
import java.util.*;
public class wellFormed
{
public static void main(String []args)
{
String str="[[{]{(())}}]]";
if(isBalanced(str))
System.out.println("balanced");
else
System.out.println("not balanced");
}
public static boolean isBalanced(String str)
{
Stack<Character> s=new Stack<Character>();
for(int i=0;i<str.length();++i)
{
if(str.charAt(i)=='{' || str.charAt(i)=='(' || str.charAt(i)=='[')
s.push(str.charAt(i));
if(str.charAt(i)=='}' || str.charAt(i)==')' || str.charAt(i)==']')
if(!matchParenthesis(s.pop(),str.charAt(i)))
return false;
}
if(!s.isEmpty())
return false;
return true;
}
public static boolean matchParenthesis(char a,char b)
{
if((a=='{' && b=='}') || (a=='(' && b==')') || (a=='[' && b==']'))
return true;
else
return false;
}
}
public bool iswellformed(string str)
{
Stack<char> s = new Stack<char>();
if (str.Length == 0)
return false;
for (int i = 0; i < str.Length; i++)
{
if ((str[i]=='{') || (str[i]=='[') || (str[i]=='('))
{
s.Push(str[i]);
}
else if ((str[i] == '}') || (str[i] == ']') || (str[i] == ')'))
{
if (s.Count == 0)
return false;
char pop = s.Pop();
if ((str[i] != '}' && pop=='{') || ((str[i] != ']') && pop =='[') || ((str[i] != ')')&& pop=='('))
return false;
}
}
if (s.Count != 0)
return false;
return true;
}
Hi Bru,
Such vivid info on the
Amazon Interview Question for Software Engineer in Tests ! Flabbergasted! Thank you for making the read a smooth sail!
The Make Public function in S3 is perfectly working these past few months and years I should say.
Just last week, we are experiencing failure in the "Make Public" function. Once I successfully upload a folder with multiple files) then Make Public the uploaded files, the notification below shows Failure
Anyways great write up, your efforts are much appreciated.
MuchasGracias,
Morgan
- R May 02, 2011