Open In App

Google STEP Intern Interview Experience

Last Updated : 03 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The shortlisting was done through our university based on our resume and academic performance. I was shortlisted and had two interviews of 45 minutes, each with a break of fifteen minutes in between. Both of them were technical rounds involving hands-on coding in a shared google doc while I was on a video call through Hangouts with the interviewer from Google.

Round 1: 

Question: A boolean expression is given in the form of a string. It contains one variable x; logical operators – ‘and’, ‘or’ and relational operators – ‘>’, ‘<‘ (there is no >= or <=). Find if the expression always evaluates to False. If yes, output False, otherwise if there exists at least one x such that the given expression can be true, output true.

Example:

1. “x<0 and x>5”

Output – False

Explanation – This can never be true as there is no ‘x’ such as x<0 and x>5. So, the given boolean expression always evaluates to false.

2. “x>0 or x<-1”

Output – True

Explanation – We have at least one ‘x’ for which given boolean expression evaluates to true. For example, put x=2 in the given expression, and it evaluates to true.

Hint: Whenever there is only ‘or’ in the boolean expression, the result is always true. (Eg: x>0 or x<0 – There exists some x such that this is true and whatever be the latter part of the expression, it evaluates to true as only ‘or’ is present. If there is no ‘or’ present (only ‘and’ is there), then we check for the expressions – if you find at least two contradicting expressions as in example 1 (that is their solution sets are disjoint), then the output is False (as we have only ‘and’ logical operation which evaluates to False unless all the expressions are True), otherwise it is True.

I have no idea how to approach the problem when both ‘and, ‘ ‘or’ are present in the expression, and I could not find such a problem anywhere on the internet.

I request someone who read this article to contribute the code to this problem kindly. (preferably in C++)




//Write Java code here
  
public class BooleanExp {
  
    public static void main(String[] args) {
        String exp1 = "x<0 and x<-5 and x>100";
        String exp2 = "x<0 and x<5";
        String exp3 = "x>5 and x<0";
        String exp4 = "x<0 or x>5";
        String exp5 = "x>5 and x<0 and x<100";
        String exp6 = "x<-100 and x>100";
  
        GetDomain(exp1);
        GetDomain(exp2);
        GetDomain(exp3);
        GetDomain(exp4);
        GetDomain(exp5);
        GetDomain(exp6);
    }
  
    public static void GetDomain(String exp) {
        if (exp.contains("or")) {
            System.out.println("true");
            return;
        }
        String subExp[] = exp.split(" ");
        int domain[] = new int[10];
        String strDomain = "";
        Boolean flag = true;
        int value;
        for (int i = 0; i < subExp.length; i++) {
            if (!subExp[i].equals("and")) {
                if (subExp[i].charAt(1) == '<') {
  
                    if (subExp[i].charAt(0) == 'x') {
                        if (strDomain.isEmpty())
                            strDomain = "<" + subExp[i].substring(2);
                        else if (strDomain.charAt(0) == '>' && Integer.parseInt(subExp[i].substring(2)) < Integer
                                .parseInt(strDomain.substring(1))) {
                            flag = false;
                            break;
                        }
                    } else {
                        if (strDomain.isEmpty())
                            strDomain = ">" + subExp[i].substring(2);
                        else if (strDomain.charAt(0) == '<' && Integer.parseInt(subExp[i].substring(2)) > Integer
                                .parseInt(strDomain.substring(1))) {
                            flag = false;
                            break;
                        }
                    }
  
                } else if (subExp[i].charAt(1) == '>') {
  
                    if (subExp[i].charAt(2) == 'x') {
                        if (strDomain.isEmpty())
                            strDomain = "<" + subExp[i].substring(2);
                        else if (strDomain.charAt(0) == '>' && Integer.parseInt(subExp[i].substring(2)) < Integer
                                .parseInt(strDomain.substring(1))) {
                            flag = false;
                            break;
                        }
                    } else {
                        if (strDomain.isEmpty())
                            strDomain = ">" + subExp[i].substring(2);
                        else if (strDomain.charAt(0) == '<' && Integer.parseInt(subExp[i].substring(2)) > Integer
                                .parseInt(strDomain.substring(1))) {
                            flag = false;
                            break;
                        }
                    }
  
                }
            }
        }
        System.out.println(flag);
    }
  
}
  
// This code is contributed by Arsalaan Javed


Output:
false
true
false
true
false
false

Round 2: 

Question: Given a string, find the minimum number of cuts to split the string so that all the resulting substrings are palindromes.

Example: “google”

Output: 2

Explanation: Minimum number of cuts to partition “google” into palindromes = 2 that is –

goog|l|e – where ‘|’ refers to a cut, the three resulting palindromes are “goog”, “l”, “e”. Therefore, the minimum number of cuts required = 2.

Hint: Refer to this article.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads