Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
public class CheckMingle {
int[][] matrix = new int[10][10];
static int count = 0;
static boolean diagChecked=false;
void initial() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 0;
}
}
}
void printMingle() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
void isMingo(int[][] matrix) {
int value = 0;
int row = 0;
int col = 0;
int data = 0;
for (int i = 0; i < 100; i++) {
value = (int) (Math.random() * 100);
row = value / 10;
col = value - row * 10;
while (matrix[row][col] != 0) {
value = (int) (Math.random() * 100);
row = value / 10;
col = value - row * 10;
}
data = (int) (Math.random() * 1000);
matrix[row][col] = data;
checkMingle(matrix, row, col);
}
}
void checkMingle(int[][] matrix, int row, int col) {
// check row
int index = 0;
while (index < matrix[row].length && matrix[row][index] != 0) {
index++;
}
if (index == matrix[row].length) {
count++;
System.out.println("Number " + count + " mingle"+" row:"+row);
}
// check column
index = 0;
while (index < matrix.length && matrix[index][col] != 0) {
index++;
}
if (index == matrix.length) {
count++;
System.out.println("Number " + count + " mingle"+" col: "+col);
}
//check diag
if(!diagChecked){
index=0;
while(index<matrix.length&&matrix[index][index]!=0){
index++;
}
if(index==matrix.length){
count++;
System.out.println("Number " + count + " mingle"+" row:"+row+" col: "+col+"It is diag!!!!");
diagChecked=true;
}
}
}
public static void main(String[] args) {
CheckMingle cm = new CheckMingle();
cm.initial();
cm.isMingo(cm.matrix);
cm.printMingle();
}
}
This code did not make sure the value in each cell of matrix is unique, but since the value is from 0 to 1000, I test it and did not see cases that two cells have same value.
Anyone who got questions like this have an opinion on my concern?
Mingo mingo = new Mingo();
mingo.Init();
Random r = new Random(100);
for (int i = 0; i < 100; i++)
{
mingo.setMingoValue(r.Next(1, 100));
mingo.isMingo();
}
mingo.cs
============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Mingo
{
int [,] m_MingoTable = new int[10,10];
public void Init()
{
for(int i = 0; i < 10; i++)
for(int j =0; j<10;j++)
{
m_MingoTable[i, j] = 0;
}
}
public void setMingoValue(int value)
{
//sanity
if(value < 100)
{
int row = System.Math.Abs(value/10);
int col =value - row*10;
m_MingoTable[row, col] = value; //use true or false?
}
//else raise error
}
public void isMingo()
{
bool bRowMingo = true, bColumnMingo = true, bDiagonalMingo = true, bOppositeDiagMingo = true;
for(int i = 0; i < 10;i++)
{
for(int j=0;j<10;j++)
{
//all column for one row
if (m_MingoTable[i, j] == 0)
bRowMingo = false;
//column - all row
if (m_MingoTable[j, i] == 0)
bColumnMingo = false;
//diagnal (0,0), (1,1), etc..
if (i == j && m_MingoTable[i, j] == 0)
bDiagonalMingo = false;
//(9,0), (8,1),(7,2) etc,
if (((i + j) == (10 - 1)) && m_MingoTable[i, j] == 0)
bOppositeDiagMingo = false;
}
if(bRowMingo == true)
Console.WriteLine(i);
if(bColumnMingo == true)
Console.WriteLine(i);
bRowMingo = bColumnMingo = false;
}
if(bDiagonalMingo == true)
Console.WriteLine("same number diagnal");
if(bOppositeDiagMingo == true)
Console.WriteLine("Sum of coordinates diagonal");
}
}
}
Mingo mingo = new Mingo();
mingo.Init();
Random r = new Random(100);
for (int i = 0; i < 100; i++)
{
mingo.setMingoValue(r.Next(1, 100));
mingo.isMingo();
}
mingo.cs
============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Mingo
{
int [,] m_MingoTable = new int[10,10];
public void Init()
{
for(int i = 0; i < 10; i++)
for(int j =0; j<10;j++)
{
m_MingoTable[i, j] = 0;
}
}
public void setMingoValue(int value)
{
//sanity
if(value < 100)
{
int row = System.Math.Abs(value/10);
int col =value - row*10;
m_MingoTable[row, col] = value; //use true or false?
}
//else raise error
}
public void isMingo()
{
bool bRowMingo = true, bColumnMingo = true, bDiagonalMingo = true, bOppositeDiagMingo = true;
for(int i = 0; i < 10;i++)
{
for(int j=0;j<10;j++)
{
//all column for one row
if (m_MingoTable[i, j] == 0)
bRowMingo = false;
//column - all row
if (m_MingoTable[j, i] == 0)
bColumnMingo = false;
//diagnal (0,0), (1,1), etc..
if (i == j && m_MingoTable[i, j] == 0)
bDiagonalMingo = false;
//(9,0), (8,1),(7,2) etc,
if (((i + j) == (10 - 1)) && m_MingoTable[i, j] == 0)
bOppositeDiagMingo = false;
}
if(bRowMingo == true)
Console.WriteLine(i);
if(bColumnMingo == true)
Console.WriteLine(i);
bRowMingo = bColumnMingo = false;
}
if(bDiagonalMingo == true)
Console.WriteLine("same number diagnal");
if(bOppositeDiagMingo == true)
Console.WriteLine("Sum of coordinates diagonal");
}
}
}
Rather than checking the entire board on each move, it would be faster to check the location of each move for membership in a full diagonal, row, or column, then call out "first, second..." etc. as a contiguous row is found. This way, you only have to keep track of the number of Mingos detected, and most move checks will terminate very quickly when they encounter an empty cell.
Mingo mingo = new Mingo();
mingo.Init();
Random r = new Random(100);
for (int i = 0; i < 100; i++)
{
mingo.setMingoValue(r.Next(1, 100));
mingo.isMingo();
}
mingo.cs
============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Mingo
{
int [,] m_MingoTable = new int[10,10];
public void Init()
{
for(int i = 0; i < 10; i++)
for(int j =0; j<10;j++)
{
m_MingoTable[i, j] = 0;
}
}
public void setMingoValue(int value)
{
//sanity
if(value < 100)
{
int row = System.Math.Abs(value/10);
int col =value - row*10;
m_MingoTable[row, col] = value; //use true or false?
}
//else raise error
}
public void isMingo()
{
bool bRowMingo = true, bColumnMingo = true, bDiagonalMingo = true, bOppositeDiagMingo = true;
for(int i = 0; i < 10;i++)
{
for(int j=0;j<10;j++)
{
//all column for one row
if (m_MingoTable[i, j] == 0)
bRowMingo = false;
//column - all row
if (m_MingoTable[j, i] == 0)
bColumnMingo = false;
//diagnal (0,0), (1,1), etc..
if (i == j && m_MingoTable[i, j] == 0)
bDiagonalMingo = false;
//(9,0), (8,1),(7,2) etc,
if (((i + j) == (10 - 1)) && m_MingoTable[i, j] == 0)
bOppositeDiagMingo = false;
}
if(bRowMingo == true)
Console.WriteLine(i);
if(bColumnMingo == true)
Console.WriteLine(i);
bRowMingo = bColumnMingo = false;
}
if(bDiagonalMingo == true)
Console.WriteLine("same number diagnal");
if(bOppositeDiagMingo == true)
Console.WriteLine("Sum of coordinates diagonal");
}
}
}
If additional memory is allowed we can use 2 arrays one for rows and one for columns and two variables for keeping track of diagonals......
- iamthe0ne October 29, 2012Increase the count at row index by one and the same for columns...and increase the diagonal count if i == j
also to keep track of repeated random nos we can always find the index of the no which is generated and check at that index if it is present already or not....to find the index of the no divide the no by n the row will the quotient and the column will be the remainder..if the remainder is 0 then place it in the last column with the row being the quotient....