Oracle Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
static Map<Integer, char[][]> map = null;
static {
char[][] zero = new char[3][3];
zero[0][0]=' ';zero[0][1]='_';zero[0][2]=' ';
zero[1][0]='|';zero[1][1]=' ';zero[1][2]='|';
zero[2][0]='|';zero[2][1]='_';zero[2][2]='|';
char[][] one = new char[3][3];
one[0][0]=' ';one[0][1]=' ';one[0][2]=' ';
one[1][0]=' ';one[1][1]='|';one[1][2]=' ';
one[2][0]=' ';one[2][1]='|';one[2][2]=' ';
char[][] two = new char[3][3];
two[0][0]=' ';two[0][1]='_';two[0][2]=' ';
two[1][0]=' ';two[1][1]='_';two[1][2]='|';
two[2][0]='|';two[2][1]='_';two[2][2]=' ';
map = new HashMap<Integer, char[][]>();
map.put(0, zero);map.put(1, one);map.put(2, two);
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
int[] toPrint = {0,1,2};
for(int i=0; i<=2; i++){
for(int j =0; j <toPrint.length; j++){
char[][] arr = map.get(toPrint[j]);
for(int k=0; k<=2;k++){
sb.append(arr[i][k]);
}
} sb.append('\n');
}
System.out.println(sb.toString());
}
Wow there is no Computer Engineer in this form? its just 7 segment display. have a number of 7 bits which toggles the respected segment when it is 1 or 0. that is it!
public class SevenSegmentDisplay
{
// Class private variable:
private short segments = 9;
private short[][] digits;
private short[][] map = {{0, 1, 0, 1, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 0, 0, 1},
{0, 1, 0, 0, 1, 1, 1, 1, 0}, {0, 1, 0, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 1, 1, 1, 0, 0, 1}, {0, 1, 0, 1, 1, 0, 0, 1, 1},
{0, 1, 0, 1, 1, 0, 1, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 0, 1},
{0, 1, 0, 1, 1, 1, 1, 1, 1}, {0, 1, 0, 1, 1, 1, 0, 1, 1}};
// Constructor:
public SevenSegmentDisplay(String numberStr)
{
digits = new short[numberStr.length()][segments];
for (int i = 0; i < numberStr.length(); i++)
{
int number = Character.getNumericValue(numberStr.charAt(i));
digits[i] = map[number];
}
display(digits);
}
// display Method:
private void display(short[][] digits)
{
String symbol;
int segment = 0;
boolean line1 = true;
while (segment <= 7)
{
for (int i = 0; i <= (digits.length - 1); i++)
{
for (int j = segment; j <= (segment + 2); j++)
{
if (line1) symbol = "_"; else if (j == (segment + 1)) symbol = "_"; else symbol = "|";
if (digits[i][j] == 0) System.out.print(" "); else System.out.print(symbol);
}
}
line1 = false;
System.out.println("");
segment = segment + 3;
}
}
// Client code:
public static void main (String[] args)
{
SevenSegmentDisplay test = new SevenSegmentDisplay("35464565465456125468712128165482610520078078011012413958");
}
}
Good idea.
A slightly different idea I was thinking of is to do the following:
+ Provide numbers to each part of the display i.e.
1 => _
2 => |
3 => ...
+ For each number, set the "number" combination to display.
This is not as easy to code as your alternative, but will save some space by avoiding duplicate strings. For example, have
one number to represent "\0\0\0\0\0".
You can safely ignore the last lines containing NULL chars, they were meant for other stuff, which I had no time to implement(character unerlining, etc). Moreover, we can reduce the width of the chars printed by using only 4 clumns instead of 5.
Sample output is below (hope the edit box will not screw it :))
_ _ _ _ _ _ _ _
| | | _| _| |_| |_ |_ | |_| |_|
|_| | |_ _| | _| |_| | |_| _|
We can divide the String into characters. For each character we give corresponding mappings like the above format. It is not the easy way?
- MahmoudMheisen91 March 13, 2014