Two Sigma Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

#include<iostream>
#include<string>
#include<stdlib.h>

using namespace std;

bool IsMatchRegexSources(const char * regex1, const char *  regex2)
{
	if (!*regex2)
		return !(*regex1);

	if (regex2[1] == '*')
		return IsMatchRegexSources(regex1, regex2 + 2) || ((*regex2 == '?' && *regex1) || (*regex1 == *regex2));

	if (*regex2 == '?')
		return *regex1 && IsMatchRegexSources(regex1 + 1, regex2 + 1);

	if (*regex2 == '*')
		return *regex1 && *regex2;

	return (*regex1 == *regex2) && IsMatchRegexSources(regex1 + 1, regex2 + 1);
}

int main()
{
	IsMatchRegexSources("abc", "a?c") ? cout << "TRUE" << endl : cout << "FALSE" << endl;
	IsMatchRegexSources("abc", "a*?c") ? cout << "TRUE" << endl : cout << "FALSE" << endl;
	IsMatchRegexSources("abc", "*") ? cout << "TRUE" << endl : cout << "FALSE" << endl;
	IsMatchRegexSources("abc", "?c") ? cout << "TRUE" << endl : cout << "FALSE" << endl;

	return 0;
}

- Basu May 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpDemo {

	public static void main(String[] args) {
		
		String word = "abcd";
		String[] regex = {"a?c","a*?c","\'*","\\?c"};
		
		for(String str: regex){
			Pattern pattern = Pattern.compile(str);
			Matcher match = pattern.matcher(word);
			System.out.println(match.find());	
		}
		

	}

}

- RockStar May 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) // wild card compare
{
while (!(*s1 == NULL && *s2 == NULL))
{
if (*s2 == '*')
{
s2++;
while (*s1 != *s2)
s1++;
}
else if (*s2 == '?' || *s1 == *s2)
{
s2++;
s1++;
}
else
return false;
}
return true;
}

- Anonymous June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2)
{
while (!(*s1 == NULL && *s2 == NULL))
{
if (*s2 == '*')
{
s2++;
while (*s1 != *s2)
s1++;
}
else if (*s2 == '?' || *s1 == *s2)
{
s2++;
s1++;
}
else
return false;
}
return true;
}

- Anonymous June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
bool strcmp(char* s1, char* s2)
{
while (!(*s1 == NULL && *s2 == NULL))
{
if (*s2 == '*')
{
s2++;
while (*s1 != *s2)
s1++;
}
else if (*s2 == '?' || *s1 == *s2)
{
s2++;
s1++;
}
else
return false;
}
return true;
}
}

- Anonymous June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsMatchRegexSources(char* s1, char* s2)  
	{
		while (!(*s1 == NULL && *s2 == NULL))
		{
			if (*s2 == '*')
			{
				s2++;
				while (*s1 != *s2)
					s1++;
			}
			else if (*s2 == '?' || *s1 == *s2)
			{
				s2++;
				s1++;
			}
			else
				return false;
		}
		return true;
	}

- Anonymous June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) //  wild card compare
	{
		while (!(*s1 == NULL && *s2 == NULL))
		{
			if (*s2 == '*')
			{
				s2++;
				while (*s1 != *s2)
					s1++;
			}
			else if (*s2 == '?' || *s1 == *s2)
			{
				s2++;
				s1++;
			}
			else
				return false;
		}
		return true;
	}

- Anonymous June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) // wild card compare
{
while (!(*s1 == NULL && *s2 == NULL))
{
if (*s2 == '*')
{
s2++;
while (*s1 != *s2)
s1++;
}
else if (*s2 == '?' || *s1 == *s2)
{
s2++;
s1++;
}
else
return false;
}
return true;
}

- moiskim June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) //  wild card compare
	{
		while (!(*s1 == NULL && *s2 == NULL))
		{
			if (*s2 == '*')
			{
				s2++;
				while (*s1 != *s2)
					s1++;
			}
			else if (*s2 == '?' || *s1 == *s2)
			{
				s2++;
				s1++;
			}
			else
				return false;
		}
		return true;

}

- moiskim June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) //  wild card compare
	{
		while (!(*s1 == NULL && *s2 == NULL))
		{
			if (*s2 == '*')
			{
				s2++;
				while (*s1 != *s2)
					s1++;
			}
			else if (*s2 == '?' || *s1 == *s2)
			{
				s2++;
				s1++;
			}
			else
				return false;
		}
		return true;
	}

- moiskim June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) //  wild card compare
	{
		while (!(*s1 == NULL && *s2 == NULL))
		{
			if (*s2 == '*')
			{
				s2++;
				while (*s1 != *s2)
					s1++;
			}
			else if (*s2 == '?' || *s1 == *s2)
			{
				s2++;
				s1++;
			}
			else
				return false;
		}
		return true;
	}

- moiskim June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool strcmp(char* s1, char* s2) //  wild card compare
	{
		while (!(*s1 == NULL && *s2 == NULL))
		{
			if (*s2 == '*')
			{
				s2++;
				while (*s1 != *s2)
					s1++;
			}
			else if (*s2 == '?' || *s1 == *s2)
			{
				s2++;
				s1++;
			}
			else
				return false;
		}
		return true;
	}

- moiskim June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import unittest

def regex_match(st, reg):
	i = 0
	j = 0
	reg_len = len(reg)
	while i < len(st) and j < reg_len:
		if reg[j].isalpha():
			if st[i] != reg[j]:
				return False
			i = i + 1
			j = j + 1
		elif reg[j] == '?':
			i = i + 1
			j = j + 1
		elif reg[j] == '*':
			if j == reg_len - 1:
				i = i + 1
			else:
				j = j + 1
	return True

class Test(unittest.TestCase):
	in_out = [['abc', 'a?c', True], ['abc', 'a*?c', True], ['abc', '*', True], ['abc', '?c', False], ['abc', 'a*', True]]

	def test_regex_match(self):
		for st, reg, out in self.in_out:
			output = regex_match(st, reg)
			self.assertEqual(output, out)

if __name__ == '__main__':
	unittest.main()

- Joseph November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import unittest

def regex_match(st, reg):
	i = 0
	j = 0
	reg_len = len(reg)
	while i < len(st) and j < reg_len:
		if reg[j].isalpha():
			if st[i] != reg[j]:
				return False
			i = i + 1
			j = j + 1
		elif reg[j] == '?':
			i = i + 1
			j = j + 1
		elif reg[j] == '*':
			if j == reg_len - 1:
				i = i + 1
			else:
				j = j + 1
	return True

class Test(unittest.TestCase):
	in_out = [['abc', 'a?c', True], ['abc', 'a*?c', True], ['abc', '*', True], ['abc', '?c', False], ['abc', 'a*', True]]

	def test_regex_match(self):
		for st, reg, out in self.in_out:
			output = regex_match(st, reg)
			self.assertEqual(output, out)

if __name__ == '__main__':
	unittest.main()

- Joseph November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import unittest

def regex_match(st, reg):
i = 0
j = 0
reg_len = len(reg)
while i < len(st) and j < reg_len:
if reg[j].isalpha():
if st[i] != reg[j]:
return False
i = i + 1
j = j + 1
elif reg[j] == '?':
i = i + 1
j = j + 1
elif reg[j] == '*':
if j == reg_len - 1:
i = i + 1
else:
j = j + 1
return True

class Test(unittest.TestCase):
in_out = [['abc', 'a?c', True], ['abc', 'a*?c', True], ['abc', '*', True], ['abc', '?c', False], ['abc', 'a*', True]]

def test_regex_match(self):
for st, reg, out in self.in_out:
output = regex_match(st, reg)
self.assertEqual(output, out)

if __name__ == '__main__':
unittest.main()

- Joseph November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import unittest

def regex_match(st, reg):
	i = 0
	j = 0
	reg_len = len(reg)
	while i < len(st) and j < reg_len:
		if reg[j].isalpha():
			if st[i] != reg[j]:
				return False
			i = i + 1
			j = j + 1
		elif reg[j] == '?':
			i = i + 1
			j = j + 1
		elif reg[j] == '*':
			if j == reg_len - 1:
				i = i + 1
			else:
				j = j + 1
	return True

class Test(unittest.TestCase):
	in_out = [['abc', 'a?c', True], ['abc', 'a*?c', True], ['abc', '*', True], ['abc', '?c', False], ['abc', 'a*', True]]

	def test_regex_match(self):
		for st, reg, out in self.in_out:
			output = regex_match(st, reg)
			self.assertEqual(output, out)

if __name__ == '__main__':
	unittest.main()

- Joseph November 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wow, that's a lot of wrong answers. Try "abcacbcd" searching for "a*cd", which should be True.

- Anonymous January 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StringMatch {

    public static void main(String[] args) {
        System.out.println(isMatch("abc", "a*?c"));
        System.out.println(isMatch("abc", "a*c"));
        System.out.println(isMatch("abc", "a?c"));
        System.out.println(isMatch("abc", "*"));
        System.out.println(isMatch("abc", "?c"));
        System.out.println(isMatch("adfjhfdnbkdsfhjjsk", "a*j?k"));
        System.out.println(isMatch("abcacbcd", "a*cd"));
    }

    private static boolean isMatch(String s, String m) {
        boolean match = isMatch(s, m, 0, 0);
        System.out.println(s + " match " + m + " is " + match);
        return match;
    }

    private static boolean isMatch(String s, String m, int ms, int ss) {

        if (ms > 0 && ms >= m.length()) {
            return true;
        }

        char mChar = m.charAt(ms);

        if (mChar == '?') {
            return isMatch(s, m, ms + 1, ss + 1);
        }

        if (mChar != '*') {
            if (s.charAt(ss) == mChar) {
                return isMatch(s, m, ms + 1, ss + 1);
            }
            return false;
        }

        return isMatch(s, m, ms + 1, s.length() - (m.length() - ms) + 1);
    }

}

- bonz May 06, 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