Thursday, July 23, 2009

Some Java Programming Interview Questions

I am going to add some Java programming questions that I have come across in interviews recently. I have tried to provide solutions but of course there might be better ways to do them...

Check if given string contains a valid FID number. If it is not valid, return "invalid". If it is valid, convert the string to one of two formats depending on the input valid of EntityType:

i) If the EntityType value is P (Personal), convert the string to "XXX-XX-XXXX" format.
ii) If the EntityType value is B (Business), convert the string to "XX-XXXXXXX" format.

You can assume the EntityType is set correctly to either P or B.

Valid input formats when EntityType = P are: 123-44-9876, 123449876, 123 44 9876

If the user inputs any other format than the above 3, the program should return Invalid entry. For example, 123-44 9876 is invalid.

Valid input formats when EntityType = B are: 98-1234567, 981234567, 98 1234567, 981-23-4567


Examples
222-44-9876 and EntityType=P -> 222-44-9876
222-44-9876 and EntityType=B -> 22-2449876


package com.programming.interview;

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

public class FIDChecker {

public static String checkFID(String strFID, String entityType ){

String returnFID = "INVALID FID/ENTITY TYPE";
entityType = ("P".equalsIgnoreCase(entityType)||"B".equalsIgnoreCase(entityType))? entityType:null;

if(null!=strFID && null!= entityType)
{
if (validateFID(strFID, entityType))
{
returnFID = reformatFID(strFID, entityType);
}
}
return returnFID;
}

private static boolean validateFID(String strFID, String entityType){
boolean isValidFID = false;
String validFormat1 = null;
String validFormat2 = null;

if ("P".equalsIgnoreCase(entityType))
{
validFormat1 = "\\d{3}-?\\d{2}-?\\d{4}";
validFormat2 = "\\d{3}\\s?\\d{2}\\s?\\d{4}";
}
else if ("B".equalsIgnoreCase(entityType))
{
validFormat1 = "\\d{2}-?\\d{7}";
validFormat2 = "\\d{2}\\s?\\d{7}";
}

Pattern matchingPattern1 = Pattern.compile(validFormat1);
Matcher patternMatcher1 = matchingPattern1.matcher(strFID);

Pattern matchingPattern2 = Pattern.compile(validFormat2);
Matcher patternMatcher2 = matchingPattern2.matcher(strFID);

if (patternMatcher1.matches() || patternMatcher2.matches())
{
isValidFID = true;
}
return isValidFID;
}

private static String reformatFID(String strFID, String entityType){

String plainFID = strFID.replaceAll("[-\\s]", "");

StringBuffer formattedFID = new StringBuffer();
if("P".equalsIgnoreCase(entityType))
{
formattedFID.append(plainFID.substring(0, 3));
formattedFID.append("-");
formattedFID.append(plainFID.substring(3, 5));
formattedFID.append("-");
formattedFID.append(plainFID.substring(5, 9));
}
else if ("B".equalsIgnoreCase(entityType))
{
formattedFID.append(plainFID.substring(0, 2));
formattedFID.append("-");
formattedFID.append(plainFID.substring(2, 9));
}

return formattedFID.toString();
}
}

1 comment:

  1. Anonymous4:38 PM

    This comment has been removed by a blog administrator.

    ReplyDelete