Flipkart Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

in :

11
ab ab
a*b aaaaaab
a*b*c* abc
a*b*c aaabccc
^abc*b abccccb
^abc*b abbccccb
^abcd$ abcd
^abc*abc$ abcabc
^abc.abc$ abczabc
^ab..*abc$ abyxxxxabc
^abc*b abbccccb

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <ctype.h>

using namespace std;

int main()
{
	//freopen("A-large-practice.in", "r", stdin);
	//freopen("A-small-practice.in", "r", stdin);
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);

	int n;
	scanf("%d", &n);
	while(n--)
	{
		char regex[100], expression[100], c, temp;
		scanf("%s", regex);
		scanf("%s", expression);
		int i =0 ,j = 0,flag = 1;
		while(regex[i] != '\0' && flag == 1)
		{
			c = regex[i];
			switch(c)
			{
			case '^': 
				i += 1;
				break;
			case '$':
				i += 1;
				break;
			case '*':
				temp = regex[i-1];
				if(temp == '.')
					temp = expression[j-1];
				while(temp == expression[j])
					j += 1;
				i += 1;
				break;
			case '.':
				if(isalpha(expression[j]))
				{
					j += 1;
					i += 1;
				}
				else
					flag = 0;
				break;
			default:
				if(regex[i] == expression[j])
				{
					i += 1;
					j += 1;
				}
				else
					flag = 0;
			}
		}

		if(flag == 0)
			printf("false\n");
		else if(regex[i] == '\0' ^ expression[j] == '\0')
			printf("false\n");
		else
			printf("true\n");
	}


	return 0;
}

- Anonymous August 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* match: search for regexp anywhere in text */
int match(char *regexp, char *text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do { /* must look even if string is empty */
if (matchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}

/* matchhere: search for regexp at beginning of text */
int matchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
return matchhere(regexp+1, text+1);
return 0;
}

/* matchstar: search for c*regexp at beginning of text */
int matchstar(int c, char *regexp, char *text)
{
do { /* a * matches zero or more instances */
if (matchhere(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}

- nk July 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Failing for the below case:-
^abc*b, abbccccb

In this case it is returning true.

- hulk August 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here iw working python code

def check_regex(reg, exp):
    if reg[0] == '^':
        reg = reg[1:]
    elif reg[-1] == '$':
        reg = reg[:-1]
    i = 0
    j = 0
    while i<len(exp):
        if reg[j] == '.':
            i += 1
        elif reg[j] == '*':
            while(exp[i-1] == exp[i]):
                i += 1
        elif exp[i] == reg[j]:
                i += 1
        else:
            return False
        j += 1
    return True

- satyans24 August 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
int expressionvalidator(char *,char *,int);
int main()
{
	char exp[100],tstcse[100];
	int s;
	printf("Enter the expression\n");
	gets(exp);
	printf("test case\n");
	gets(tstcse);
	//char exp1='o',tstcse1='p';
	//printf("%d",strlen(exp));
	s=expressionvalidator(exp,tstcse,strlen(exp));
	switch(s)
	{
	case 1: printf("true");
			break;
	case 0: printf("false");
	}
}
int expressionvalidator(char *exp,char *tstcse,int length)
{
	char *ptr,*tpt;int i;
	//printf("%c and %c",*exp,*tstcse);
	if(*exp == '^')
	{
		exp++;
		length--;
	}
	if(*exp == '*' || *exp == '$')
	return 0;
	while((*exp) != '\0')
	{
		
		for(i=0;i<length;i++)
		{
			//printf("Equal1");
			if((*exp) == (*tstcse))
			{
				ptr=exp;
				exp++;
				tstcse++;
				//printf("Equal");
			}
			else if( (*exp) == '*') 
			{
				
				if(*ptr == *tstcse || *ptr == '.')
				{
					if(*ptr == *tstcse)
					{
					while(*ptr == *tstcse)
					{
						tstcse++;
					}
					ptr = exp;
					exp++;
					}
					else
					{
						while(*tpt == *tstcse)
						{
							tstcse++;
						}
						ptr = exp;
						exp++;
					}
					
				}
				else 
				{
					exp++;
					if(*exp == *tstcse)
					{
						ptr=exp;
						exp++;
						tstcse++;
					}
					else
					{
						return 0;
					}
				}
			}
			else if( (*exp) == '.')
			{
					tpt = tstcse;
					ptr=exp;
					exp++;
					tstcse++;
			}
			else if(*exp == '$')
			{
				return 1;
			}
			else
			{
				return 0;
			}
			
		}
	}
	return 1;
}

- Yagyesh Tripathi November 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Workable java code with comments -

public class StringMatchingRegex {

    public static void main(String args[]) {
        String tests[][] = { {"ab", "ab" },
                             {"a*b" , "aaaaaab"},
                             {"a*b*c*", "abc"},
                             {"a*b*c" , "aaabccc"},
                             {"^abc*b", "abccccb"},
                             {"^abc*b", "abbccccb"},
                             {"^abcd$", "abcd"},
                             {"^abc*abc$" , "abcabc"},
                             {"^abc.abc$", "abczabc"},
                             {"^ab..*abc$", "abyxxxxabc"}                            
        };
        
        for (int i = 0; i < tests.length; i++) {
        boolean yes = matches(tests[i][0] , tests[i][1]);
        System.out.println(tests[i][0] +" "+ tests[i][1] + " = " + yes);
        }
    }

    private static boolean matches(String regex, String string) {
        if (regex == null || string == null) {
            return false;
        }
        
        boolean result = true;
        int k = 0;
        for (int i = 0; i < regex.length(); i++) {
            char c = regex.charAt(i);
            if (isAlpha(regex.charAt(i))) {
                if (k < string.length() && string.charAt(k++) == regex.charAt(i)) {
                    continue;
                } else {
                    result = false;
                    break;
                }
            } else {
                k = handle(string, k, c);
                if (k == -1 ) {
                    result = false;
                    break;
                }
            }
        }
        
        if (k < string.length()) {
            //there is still unmatched string
            result = false;
        }

        return result;
    }

    private static boolean isAlpha(char c) {
        return Character.isLetter(c);
    }

    private static int handle(String string, int i, char c) {
        switch (c) {
            case '.':
                //match a character
                if (i >= string.length()) {
                    return -1;
                }
                return ++i;
            case '?' :
                //match 0 or 1 character
                if (i < string.length() && string.charAt(i) == string.charAt(i-1)) {
                    return ++i;
                } else {
                    return i;
                }
            case '+':
                //one or more
                int count = 0;
                char prev = string.charAt(i-1);
                while (i < string.length() && string.charAt(i) == prev ) {
                    count++; i++;
                }
                if (count >= 1) {
                    return i;
                } else {
                    return -1;
                }                
            case '*':
                //match 0 or more characters
                if (i >= string.length()) {
                    return i;
                }
                count = 0;
                prev = string.charAt(i-1);
                while (i < string.length() && string.charAt(i) == prev ) {
                    count++; i++;
                }
                if (count >= 0) {
                    return i;
                } else {
                    return -1;
                } 
            case '^':
                if (i!=0) {
                    throw new IllegalArgumentException("Invalid regex");
                }
                return i++;
            case '$':
                if (i == string.length()) {
                    return ++i;
                } else {
                    return -1;
                }
        }

        //some unknown character
        return -1;
    }
}

- P November 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is from a programming contest/website.

- Anonymous March 19, 2014 | 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