Interview Question


Country: India




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

pattern = "expr1 OR expr2"

exrp1 = [A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]
exrp2 = [0-9][A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9][A-Za-z]

Limitation: special chars can't be considered here

- PKT October 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

'1abcdefg' is not covered here!

- cobra October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct... still finding sol... I guess by just reg exp we can't the sol....

- PKT October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

str.matches("[A-Za-z0-9]{8}")

- sunez.riyaz October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

'aabbccdd' is valid which is wrong.. atleast one number should be there..

- cobra October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Warning: this method is pretty inefficient, but it should work.

for i in range (8):
	for j in range(8):
		if i != j:
			for k in range (8):
				if k == i:
					string = string + r"\d"
				elif k == j:
					string = string + r"[a-zA-Z]"
				else:
					string = string + r"."
			if i != 7 or j != 6:
				string = string + r"|"

String will be your regular expression at the end

Essentially, this determines all possible positions of the two necessary characters, and creates a regular expression that takes them into account. I'd advise against this method if you can think of a better manner, but it should produce a working regex.

Edit: Another method using a couple of regexes, run

"^........$"

as your first regular expression.

After this, run the second expression which another user listed above:

".*\d+.*[a-zA-Z]+.*|.*[a-zA-Z]+.*\d+.*"

Still looking for a single expression that is more efficient than the previous one I listed, no luck so far.

- paulj91 October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

^\d[A-Za-z]......|\d.[A-Za-z].....|\d..[A-Za-z]....|\d...[A-Za-z]...|\d....[A-Za-z]..|\d.....[A-Za-z].|\d......[A-Za-z]|[A-Za-z]\d......|.\d[A-Za-z].....|.\d.[A-Za-z]....|.\d..[A-Za-z]...|.\d...[A-Za-z]..|.\d....[A-Za-z].|.\d.....[A-Za-z]|[A-Za-z].\d.....|.[A-Za-z]\d.....|..\d[A-Za-z]....|..\d.[A-Za-z]...|..\d..[A-Za-z]..|..\d...[A-Za-z].|..\d....[A-Za-z]|[A-Za-z]..\d....|.[A-Za-z].\d....|..[A-Za-z]\d....|...\d[A-Za-z]...|...\d.[A-Za-z]..|...\d..[A-Za-z].|...\d...[A-Za-z]|[A-Za-z]...\d...|.[A-Za-z]..\d...|..[A-Za-z].\d...|...[A-Za-z]\d...|....\d[A-Za-z]..|....\d.[A-Za-z].|....\d..[A-Za-z]|[A-Za-z]....\d..|.[A-Za-z]...\d..|..[A-Za-z]..\d..|...[A-Za-z].\d..|....[A-Za-z]\d..|.....\d[A-Za-z].|.....\d.[A-Za-z]|[A-Za-z].....\d.|.[A-Za-z]....\d.|..[A-Za-z]...\d.|...[A-Za-z]..\d.|....[A-Za-z].\d.|.....[A-Za-z]\d.|......\d[A-Za-z]|[A-Za-z]......\d|.[A-Za-z].....\d|..[A-Za-z]....\d|...[A-Za-z]...\d|....[A-Za-z]..\d|.....[A-Za-z].\d|......[A-Za-z]\d$

This is "OR" of all permutations of "1a......". Yes, autogenerated, readonly, but it works

- Zubaloo November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

.*[A-Za-z]+.*[0-9]+.*|.*[0-9].*[A-Za-z]+.*

There are 2 parts to this regular expression.

- betacod3r October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

about length which should be only 8??

- cobra October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@betacod3r:
-As cobra said what about length should be exactly 8 and
-if we create any regular exp which takes exactly 8 char need to take care of special char also.

- PKT October 21, 2012 | Flag


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