Walmart Labs Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

public static int nestedParenthesisDepth(String input) throws IllegalArgumentException {
    if(input == null){
        throw new IllegalArgumentException();
    }

    int openParenCount = 0;
    int maxOpenParen = 0;
    for(int i = 0; i < input.length(); i++){
        char c = input.charAt(i);
        if('(' == c){
            openParenCount++;
            if(openParenCount > maxOpenParen){
                maxOpenParen = openParentCount;
            }
        }
        else if(')' == c){
            openParenCount--;
            if(openParenCount < 0){
                throw new IllegalArgumentException();
            }
        }
    }
    if(openParenCount > 0){
        throw new IllegalArgumentException();
    }
    return maxOpenParen;
}

Edit: added fix to catch "a(b" cases. Thanks anonymous and JSDUDE

- zortlord June 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I wrote pretty much the same code (used switch case instead of if-else).
However this has a bug.
Case "a(b" is not handled.

Before you return the maxOpenParen, you need to check openParenCount is 0 or not. If not zero throw the IllegalArgumentException.

Otherwise good :)

- Anonymous June 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I wrote almost the same code (I used switch case instead of if-else).
However your code misses the scenario "a(b"

Fix:

if(openParenCount > 0) {
	throw new IllegalArgumentException();
}

return maxOpenParen;

- JSDUDE June 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getMaxParanthesisDepth(String str){
        int maxDepth=0;
        int potentialMaxDepth=0;
        int curDepth = 0;
        
        for(int i=0; i<str.length(); ++i ){
            //left brace
            if(str.charAt(i) == '('){
                curDepth++;
                potentialMaxDepth++;
            }
            //right brace
            else if(str.charAt(i) == ')')
            {
                curDepth--;
                
                //unmatched right brace 
                if(curDepth < 0){
                    System.out.println("unmatched brace");
                    maxDepth = -1;
                    break;
                }
                
                //one nest is processed
                if(curDepth == 0){
                    if(potentialMaxDepth > maxDepth){
                        maxDepth = potentialMaxDepth;
                        potentialMaxDepth=0;
                    }
                }
                
            }
        }
        
        ////unmatched left brace 
        if(curDepth != 0){
            System.out.println("Unmatched braces");
            return -1;
        }
        else return maxDepth;
    }

- X June 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution. The standard approach is to use stack.

import java.util.Stack;


public class StringUtilities {
	public static	int nestedParenthesesDepth(String input) throws IllegalArgumentException{
		
		Stack<Character> stack = new Stack<Character>();
		int depth = 0;
		int maxDepth = 0;
		
		
		for(int i=0; i<input.length();i++){
			if (input.charAt(i)=='(') {
				stack.push(input.charAt(i));
				depth++;
			}			
			if(maxDepth<depth) maxDepth = depth;
			if (input.charAt(i)==')') {
				stack.pop();	
				depth--;
			}
			
		}
		
		if(stack.size()>0) throw new IllegalArgumentException();
		return maxDepth;
		
	}

}

- Ognian.Kabranov June 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You don't even need a stack in the first place.

- monica_shankar June 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Careercup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.lang.IllegalArgumentException;

/**
 *
 * @author
 */
public class CountBracketBalance {
    
    
    static BufferedReader br;
    static StringTokenizer st;
    static int head=0,N=0,count=0;
    
    
    static ArrayList<Character> queue=new ArrayList<Character>();
    
    public static void init() throws IOException
    {
        if(st==null)
            st=new StringTokenizer(br.readLine().replace("", " "));
        
        
    }
        
    public static void push(Character ch)
    {
        if(N>=queue.size())  //cases where N==arraylist.size
        {
            queue.ensureCapacity(queue.size()*2);
            
            for(int i=N+1;i<queue.size();i++)
                queue.add(null);
            
        }
        
        //stack.set(N++, ch);
        queue.add(N++, ch);
    }
    
    public static Character pop()
    {
        if(queue.isEmpty())
        { System.err.println("Underflow operation requested, exiting");
            System.exit(0); }
        
        Character temp=queue.get(head);
        queue.set(head, null);
        head++;
        N--;
        return temp;
    }
    
    public static void main(String args[]) throws IOException
    {
        int maxcount=0;
        br=new BufferedReader(new InputStreamReader(System.in));
        init();
        while(st.hasMoreTokens())
        {
            
            //push(st.nextToken().charAt(0));
            String temp=st.nextToken();
            push(temp.charAt(0));
        }
        
        
        
        while(N>0)
        {
            Character ch=pop();
            
            if(ch=='(')
            { count++;
            maxcount=Math.max(maxcount, count);}
            
            if(ch==')')
                count--;
            
            if(count<0)
                throw new IllegalArgumentException();
            
        }
        if(count!=0)
            throw new IllegalArgumentException();
        
        
        System.out.println(maxcount);
        
        
    }    
}

- monica_shankar June 08, 2015 | 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