Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
I think the correct regex should be
^[a-zA-Z0-9_\\.]+@[a-zA-Z0-9\\.]+\\.edu$
Since there can be a '.' after or before the @sign as well.
Ideally e-mail cannot start with a dot (.) . The reg x as per the question constraints should be ^[_A-Za-z0-9-]+@[_A-Za-z0-9]+(\\.edu)$
Agreed with devdeep. We should specify that the intended algorithm is to match the input string to the regex and accept the string iff the match of the regex is exactly the whole input string. Furthermore, the regex should be told to match the maximum pattern that corresponds to the regex. That is, helloworld@stuff.educational.edu shouldn't match helloworld@stuff.edu and thus fail the test specified above.
Regex for any email id, that can ends with com edu or anything:
^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+
(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
^ #start of the line
[_A-Za-z0-9-]+ # must start with string in the bracket [ ], must contains one or more (+)
( # start of group #1
\\.[_A-Za-z0-9-]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+)
)* # end of group #1, this group is optional (*)
@ # must contains a "@" symbol
[A-Za-z0-9]+ # follow by string in the bracket [ ], must contains one or more (+)
( # start of group #2 - first level TLD checking
\\.[A-Za-z0-9]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+)
)* # end of group #2, this group is optional (*)
( # start of group #3 - second level TLD checking
\\.[A-Za-z]{2,} # follow by a dot "." and string in the bracket [ ], with minimum length of 2
) # end of group #3
$ #end of the line
I have a solution proposal which needs some attention. I am thinking of having a method for checking the valid email address which takes string as an input.
public boolean emailChecker(String s) {
boolean flag = false;
String input = s.split("@"); //split the email address from the @ onwards
//Now if input does NOT startWith("alumni") and contains("edu") then we can set the flag as true;
if(!(input.startsWith("alumni") && input.contains("edu")) { flag = true; }
return flag;
-------
The above code needs clarification. I mean is this correct or am I missing out on any information.
Thanks!!
@renegade
Your solution is correct but not complete. You are focusing on only the sample input string given in the question.
In case you want to filter out many other substrings like "alumni" etc, your code size will keep on increasing and it will be difficult to maintain that code.
We can use this regex to check the above conditions: ^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.edu$
In this regex, we check for the start to be one of more instance of [a-zA-Z0-9_]. There can be more valid characters here. i'm just including these.
This must be followed by an '@' sign which must be followed by some characters of pattern [a-zA-Z0-9] then a '.' and finally ends with an 'edu' text.
A sample java program is:
- Jester January 27, 2012