This question is asked in UBS bank Interview.
Problem:-
Berlin Clock represent time in the form of Lamps. Each Row representation of Lamps involves a
different logic. On the top of the clock there is a yellow lamp that blinks on/off every two seconds.The time is calculated by adding rectangular lamps.
The top two rows of lamps are red. These indicate the hours of a day. In the top row there are 4 red lamps. Every lamp represents 5 hours. In the lower row of red lamps every lamp represents 1 hour.So if two lamps of the first row and three of the second row are switched on that indicates 5+5+3=13h or 1 pm.
The two rows of lamps at the bottom count the minutes. The first of these rows has 11 lamps, the second 4. In the first row every lamp represents 5 minutes. In this first row the 3rd, 6th and 9th lamp are red and indicate the first quarter, half and last quarter of an hour. The other lamps are yellow. In the last row with 4 lamps every lamp represents 1 minute.
For example:-
Input Result
00:00:00 Y
OOOO
OOOO
OOOOOOOOOOO
OOOO
Sample TimeConverter Class
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class contains the business logic to convert time into Berlin Clock
*
* @author Dixit
*
*/
public class TimeConverterImpl implements TimeConverter {
/**
* Method validates and convert the time(hh:mm:ss) into Berlin Clock(in the form of Lamps)
* For example: 00:00:00 is converted into
* Y
* OOOO
* OOOO
* OOOOOOOOOOO
* OOOO
*
* @param aTime consist of String in the form of hh:mm:ss.
* @return String consisting of Lamps value.
*/
@Override
public String convertTime(String time) {
// Input time needs to validated first before performing conversion.
if (!validateInputTime(time)) {
return TimeConverterConstants.INVALID_TIME;
}
int timeUnitDivisibleByFive;
int timeUnitModulusOfFive;
int timeUnitValue;
// Input time is split using String split method providing ":" as
// argument
String[] splitTime = time.split(TimeConverterConstants.COLON);
StringBuffer convertedTime = new StringBuffer();
// first Seconds Lamp(First Row) is appended to StringBuffer.
convertedTime.append(getLamps(Integer.parseInt(splitTime[2])));
// line separator is added
convertedTime.append(TimeConverterConstants.LINE_SEPARATOR);
timeUnitValue = Integer.parseInt(splitTime[0]);
timeUnitDivisibleByFive = timeUnitValue
/ TimeConverterConstants.MULTIPLE_OF_FIVE;
timeUnitModulusOfFive = timeUnitValue
% TimeConverterConstants.MULTIPLE_OF_FIVE;
// append second row of Hour Lamps
convertedTime.append(getLamps(timeUnitValue, timeUnitDivisibleByFive,
TimeConverterConstants.RED_LAMP));
convertedTime.append(TimeConverterConstants.LINE_SEPARATOR);
// append third row of Hour Lamps
convertedTime.append(getLamps(timeUnitValue, timeUnitModulusOfFive,
TimeConverterConstants.RED_LAMP));
convertedTime.append(TimeConverterConstants.LINE_SEPARATOR);
timeUnitValue = Integer.parseInt(splitTime[1]);
timeUnitDivisibleByFive = timeUnitValue
/ TimeConverterConstants.MULTIPLE_OF_FIVE;
timeUnitModulusOfFive = timeUnitValue
% TimeConverterConstants.MULTIPLE_OF_FIVE;
// append fourth row of Minute Lamps.
convertedTime.append(getLamps(timeUnitValue, timeUnitDivisibleByFive,
TimeConverterConstants.RED_LAMP,
TimeConverterConstants.YELLOW_LAMP));
convertedTime.append(TimeConverterConstants.LINE_SEPARATOR);
// append fifth row of Minute Lamps.
convertedTime.append(getLamps(timeUnitValue, timeUnitModulusOfFive,
TimeConverterConstants.YELLOW_LAMP));
return convertedTime.toString();
}
/**
* Method Validates the Time It checks for following things:- 1) Checks for
* Null and empty value of inputTime. 2) Checks for proper valid pattern of
* time in the form of (hh:mm:ss) and also check for limit value allowed in
* HH ,MM and SS. For example: 00:00:00 is valid time but 00:00 is not.
*
* @param inputTime
* consist of time in the form of hh:mm:ss.
* @return true if input is correct and false if input is not correct.
*/
private boolean validateInputTime(String inputTime) {
try {
if (inputTime != null && !inputTime.isEmpty()) {
Pattern timeRegexPattern = Pattern
.compile(TimeConverterConstants.TIME_REGEX_PATTERN);
Matcher timeMatcher = timeRegexPattern.matcher(inputTime);
if (!timeMatcher.matches()) {
return false;
}
} else {
return false;
}
} catch (Exception e) {
System.out.println("Exception occured in validation method :-"
+ e.getMessage());
}
return true;
}
/**
* Method checks if timeUnitValue modulus of 2 is 0 then it returns YELLOW
* Lamp else it returns OFF Lamp.
*
* @param timeUnitValue
* consist of SS value.
* @return String consist of Single Lamp.
*/
@Override
public String getLamps(int timeUnitValue) {
if (timeUnitValue % TimeConverterConstants.MULTIPLE_OF_TWO == 0) {
return TimeConverterConstants.YELLOW_LAMP;
} else {
return TimeConverterConstants.OFF_LAMP;
}
}
/**
* This method returns Eleven Lamps based on the numericValue passed
* to it. The lamps result may consist of RED,YELLOW,OFF and combination of
* (RED and OFF)Lamps or (YELLOW and OFF) Lamps.
*
* @param timeUnitValue
* consist of MM value.
* @param numericValue
* consist of integer value divisible by five.
* @param strRedLamp
* consist of RED lamp value.
* @param strYellowLamp
* consist of YELLOW lamp value.
* @return String consist of Eleven Lamps.
*/
@Override
public String getLamps(int timeUnitValue, int numericValue,
String strRedLamp, String strYellowLamp) {
StringBuilder lamps = new StringBuilder(
TimeConverterConstants.ELEVEN_OFF_LAMPS);
for (int i = 0; i < numericValue; i++) {
if ((i + 1) % TimeConverterConstants.MULTIPLE_OF_THREE == 0) {
lamps.replace(i, i + 1, strRedLamp);
} else {
lamps.replace(i, i + 1, strYellowLamp);
}
}
return lamps.toString();
}
/**
* This method returns four Lamps based on the numericValue passed to
* it. The lamps result may consist of RED,YELLOW,OFF and combination of
* (RED and OFF)Lamps or (YELLOW and OFF) Lamps.
*
* @param timeUnitValue
* consist of either HH or MM value.
* @param numericValue
* consist of integer which may be divisible of five or modulus
* of five.
* @param strLamp
* lamp value passed to the function. It can be either RED or
* YELLOW.
* @return String consist of four Lamps.
*/
@Override
public String getLamps(int timeUnitValue, int numericValue, String strLamp) {
StringBuilder lamps = new StringBuilder(
TimeConverterConstants.FOUR_OFF_LAMPS);
for (int i = 0; i < numericValue; i++) {
lamps.replace(i, i + 1, strLamp);
}
return lamps.toString();
}
}
To get the full code,please hit this link Berlin Clock
Enjoy Programming :)