IBM Interview Question for Software Engineer / Developers


Country: United States




Comment hidden because of low score. Click to expand.
2
of 2 vote

public static void main(String[] args) {
		Stack<Character> ps = new Stack<Character>();
		Stack<Character> ss = new Stack<Character>();
		//List<String> list = new ArrayList<String>();=
		args= new String[]{"(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy)) "+
			"(VP (VBG eating) (NP (NNS sausages))))))"};
		char[] toArray = args[0].toCharArray();
		for (char c : toArray) {
			if(c =='('){
				ps.push('(');
			}else if(c ==')'){
				ps.pop();
				StringBuilder sb = new StringBuilder();
				while(!ss.isEmpty()){
					char s = ss.pop();
					if(s == ' '){//reverse expected
						char[] a = sb.toString().toCharArray();
						for (int i = sb.length()-1; i >=0; i--) {
							System.out.print(a[i]);
						}System.out.println();
						ss.clear();
						break;
					}else{
						sb.append(s);
					}
				}
			}else{
				ss.push(c);
			}			
		}

}

- Ashis Kumar Chanda October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Thanks ashish for your code. But the output of your code is: James
is
a
boy
eating
sausages

Instead it should be: James is a boy eating sausages.

- Abhinav October 13, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Thanks ashish for your code. But the output of your code is: James
is
a
boy
eating
sausages

Instead it should be: James is a boy eating sausages.

- abhinav.thegame October 13, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void main(String[] args) {
		Stack<Character> ps = new Stack<Character>();
		Stack<Character> ss = new Stack<Character>();
		//List<String> list = new ArrayList<String>();=
		args= new String[]{"(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy)) "+
			"(VP (VBG eating) (NP (NNS sausages))))))"};
		char[] toArray = args[0].toCharArray();
		for (char c : toArray) {
			if(c =='('){
				ps.push('(');
			}else if(c ==')'){
				ps.pop();
				StringBuilder sb = new StringBuilder();
				while(!ss.isEmpty()){
					char s = ss.pop();
					if(s == ' '){//reverse expected
						char[] a = sb.toString().toCharArray();
						for (int i = sb.length()-1; i >=0; i--) {
							System.out.print(a[i]);
						}System.out.println();
						ss.clear();
						break;
					}else{
						sb.append(s);
					}
				}
			}else{
				ss.push(c);
			}			
		}
	}

- Ashis Kumar October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Stack<Character> ps = new Stack<Character>();
Stack<Character> ss = new Stack<Character>();
//List<String> list = new ArrayList<String>();=
args= new String[]{"(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy)) "+
"(VP (VBG eating) (NP (NNS sausages))))))"};
char[] toArray = args[0].toCharArray();
for (char c : toArray) {
if(c =='('){
ps.push('(');
}else if(c ==')'){
ps.pop();
StringBuilder sb = new StringBuilder();
while(!ss.isEmpty()){
char s = ss.pop();
if(s == ' '){//reverse expected
char[] a = sb.toString().toCharArray();
for (int i = sb.length()-1; i >=0; i--) {
System.out.print(a[i]);
}System.out.println();
ss.clear();
break;
}else{
sb.append(s);
}
}
}else{
ss.push(c);
}
}
}

- Ashis Kumar October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// ZoomBA
tree =  { "NP" : [ { "DT" :  "a" } , { "NN" : "boy" } ] }
def traverse( node , s  ){
    names = list( node.keySet )
    name = names.0 // yes, we did it ourselves
    value = node[name]
    if ( value isa [ ] ){
       for ( child : value ){
          traverse ( child , s )
       }
    }else{
       s += value
    }
}
s = list()
traverse ( tree, s )
println( str(s, ' ') )

Here, we are forced to use minimal dictionary ( json ) format to store the parse tree.
Observe that the ordering does not matter at all for the nodes, because of the format chosen. Order is implicit in the list.

- NoOne October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//ZoomBA
s = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))"
def simplified_solution(s){
   // observe that every ')' previous to that is the word to add, so :
   cur = 0
   len = #|s|
   words = list()
   while ( cur < len  ){
     r = index ( [cur: len] ) :: { s[$.item] == ')' } + cur
     break ( r >= len )
     l = rindex ( [ cur:r ] ) :: { s[$.item] == ' ' } + cur
     break ( l >= len )
     w = s[l+1:r-1]
     if ( !empty( w.trim() ) ) { words += w }
     cur = r + 1
   }
   println ( str ( words , ' ' ) )
}
simplified_solution( s )

A much faster and alternative -- to do it directly from string rep.

- NoOne October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string NLPExtract(const std::string & input)
{
   std::string rval;
   bool bInWord = false;
   for (int i = 0; i < (int)input.size()-1; i++)
   {
      bInWord = bInWord ? isalnum(input[i]) : 
                (input[i] == ' ') && isalnum(input[i+1]);
      if (bInWord)
         rval += input[i];
   }
   return rval; 
}

- tjcbs2 October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String st = "(S (NP (NNP james)) (VP (VBZ is) (NP (NP (DT a)(NN boy))";
StringBuffer str = new StringBuffer(st);

for (int i = str.length()-1; i >= 0 ; i--) {
System.out.println(i);
if(str.charAt(i)=='(' || str.charAt(i)== ')'){
str.deleteCharAt(i);
} else if(Character.isUpperCase(str.charAt(i))){
str.deleteCharAt(i);
}
}
System.out.println(str);

- FlaggedActive October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringExtract {

	public static void extractString(String inp) {
		
		String[] stringPattern = inp.split(" ");

		for (int i = 0; i < stringPattern.length; i++) {
			
			String temp = stringPattern[i];

			if (!String.valueOf(temp.charAt(0)).equals("(")) {

				if (temp.contains("(")) {

					temp = temp.substring(0, temp.indexOf("("));

					System.out.println(temp.replace(")", ""));

				} else {

					System.out.println(temp.replace(")", ""));
				}

			}

		}

	}

	public static void main(String[] args) {

		String inp = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy))(VP (VBG eating) (NP (NNS sausages))))))";
		
		//String inp = "(NP(DT a) (NN boy))";
		extractString(inp);

	}

}

- Sandy0109 October 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static StringBuilder trim(String arg){

        String [] arr = arg.split("\\)");

        StringBuilder sb = new StringBuilder();

        for(String str: arr ){
            if(str.length()>0){
                sb.append(str.substring(str.lastIndexOf(" ")));
            }
        }

        return sb;
    }

    public static void main(String... args) {

        String str1 = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))";
        System.out.println(trim(str1));

        String str2 = "(NNS sausages)";
        System.out.println(trim(str2));

        String str3 = "(NP (NP (DT a) (NN boy))";
        System.out.println(trim(str3));
    }

- acm October 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static StringBuilder trim(String arg){

        String [] arr = arg.split("\\)");

        StringBuilder sb = new StringBuilder();

        for(String str: arr ){
            if(str.length()>0){
                sb.append(str.substring(str.lastIndexOf(" ")));
            }
        }

        return sb;
    }

    public static void main(String... args) {

        String str1 = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))";
        System.out.println(trim(str1));

        String str2 = "(NNS sausages)";
        System.out.println(trim(str2));

        String str3 = "(NP (NP (DT a) (NN boy))";
        System.out.println(trim(str3));
    }

- acem October 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static StringBuilder trim(String arg){

        String [] arr = arg.split("\\)");

        StringBuilder sb = new StringBuilder();

        for(String str: arr ){
            if(str.length()>0){
                sb.append(str.substring(str.lastIndexOf(" ")));
            }
        }

        return sb;
    }

    public static void main(String... args) {

        String str1 = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))";
        System.out.println(trim(str1));

        String str2 = "(NNS sausages)";
        System.out.println(trim(str2));

        String str3 = "(NP (NP (DT a) (NN boy))";
        System.out.println(trim(str3));
    }

- acm October 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main():
    outputString =""
    inputString = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))"
    inputString = inputString.strip().split(' ')
    for word in inputString:
        if word.endswith(')'):
            outputString = outputString + str(word.strip(')')) + " "
    
    print(outputString)
    
main()

- Mystery_coder November 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

String sText = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy)) "+
"(VP (VBG eating) (NP (NNS sausages))))))";

StringBuffer sBuffer = new StringBuffer();
String[] textunderTest = sText.split("\\s|\\)");

for(String string : textunderTest){

if(string.length() > 0 && string.charAt(0) != '(' ){
sBuffer.append(string + " ");
}
}

System.out.println(sBuffer.toString());
}

- karthikvaithinathan December 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		
		String sText = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy)) "+
		"(VP (VBG eating) (NP (NNS sausages))))))";
		
		StringBuffer sBuffer = new StringBuffer();
		String[] textunderTest = sText.split("\\s|\\)");
		
		for(String string : textunderTest){
			
			if(string.length() > 0 && string.charAt(0) != '(' ){
				sBuffer.append(string + " ");
			}
		}
		
		System.out.println(sBuffer.toString());
	}

- karthikvaithinathan December 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey abhinav, was this question asked in the guru interview, or the initial hirevue challenge?

- wonderkid February 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can I have something without having SPAM in it? :)
Python 3:

l = '(NP(DT a)(NN boy))'
k = l.replace('(',' ').replace(')',' ')
list1 = k.split()
list2 = []
for i in range(len(list1)):
    if list1[i].isupper():
        list1[i]= ''
s = ' '.join(list1).strip()
print(s)

- manmaybarot July 19, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More