Epic Systems Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

/*
 * There is a security keypad at the entrance of a building. It has 9 numbers 1
 * - 9 in a 3x3 matrix format. 1 2 3 4 5 6 7 8 9
 * 
 * The security has decided to allow one digit error for a person but that digit
 * should be horizontal or vertical. Example: for 5 the user is allowed to enter
 * 2, 4, 6, 8 or for 4 the user is allowed to enter 1, 5, 7.
 * 
 * IF the security code to enter is 1478 and if the user enters 1178 he should
 * be allowed.
 * 
 * Write a function to take security code from the user and print out if he
 * should be allowed or not
 */
public class SecurityKeypad {

	public final static char[][] matrix = { 
			{ '1', '2', '3' },
			{ '4', '5', '6' }, 
			{ '7', '8', '9' } 
		};

	public static boolean authenticate(String code, String userCode) {
		boolean errorFound = false;
		boolean valid = false;
		if(code.equals(userCode)){
			valid = true;
		}else if(code.length() != userCode.length()){
			valid = false;
		}
		else{
			int codeLength = userCode.length();
			for(int i=0; i<codeLength;i++){
				if(code.charAt(i) != userCode.charAt(i)){
					if(errorFound){
						return false;
					}
					for(int m = 0; m<3;m++){
						for(int n=0;n<3;n++){
							if(matrix[m][n] == code.charAt(i)){
								
									errorFound = true;
									if(checkPrev(m,n,userCode.charAt(i), code.charAt(i))){
										valid = true;
									}
									else if(checkNext(m,n,userCode.charAt(i), code.charAt(i))){
										valid = true;
									}
									else if(checkUp(m,n,userCode.charAt(i), code.charAt(i))){
										valid = true;
									}
									else if(checkDown(m,n,userCode.charAt(i), code.charAt(i))){
										valid = true;
									}
								
							}
						}
					}
					
				}
			}
		}
		return valid;
	}
	
	private static boolean checkPrev(int y, int x, char u, char c){
		int prev = x - 1;
		
		if(prev >= 0 
				&& matrix[y][prev] == u){
			return true;
		}
		return false;
	}
	
	private static boolean checkUp(int y, int x, char u, char c){
		int up = y - 1;
		
		if(up >= 0 
				&& matrix[up][x] == u){
			return true;
		}
		return false;
	} 

	private static boolean checkDown(int y, int x, char u, char c){
		int down = x + 1;
		if(down >= 0 
				&& matrix[down][x] == u){
			return true;
		}
		return false;
	} 
	
	private static boolean checkNext(int y, int x, char u, char c){
		int next = x + 1;
		
		if(next <=2 
				&& matrix[y][next] == u){
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		if(authenticate("1478", "1178")){
			System.out.println("Success!");
		}
		else{
			System.out.println("Failure!");
		}
	}

}

- tj May 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

checkDown (int y, int x, char u, char c)

Line1 of this function should be:

int down = y + 1;

(instead of x + 1)

Just a small typographical error.. otherwise does the job well.

- correction July 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

private static boolean verify(String correctPasswor, String enteredPassword) {

char[] cor=correctPasswor.toCharArray();
char[] ent=enteredPassword.toCharArray();
for(int i=0;i<cor.length;i++)
{
if(!((Math.abs((int)cor[i]-(int)ent[i])==3) || (Math.abs((int)cor[i]-(int)ent[i])==1)||(Math.abs((int)cor[i]-(int)ent[i])==0)))
{
return false;
}


}
return true;
}

- Anonymous August 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

private static boolean verify(String correctPasswor, String enteredPassword) {
// TODO Auto-generated method stub
char[] cor=correctPasswor.toCharArray();
char[] ent=enteredPassword.toCharArray();
for(int i=0;i<cor.length;i++)
{
if(!((Math.abs((int)cor[i]-(int)ent[i])==3) || (Math.abs((int)cor[i]-(int)ent[i])==1)||(Math.abs((int)cor[i]-(int)ent[i])==0)))
{
return false;
}


}
return true;
}

- Anonymous August 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Accept the code and compare each digit one by one. On the first mismatch run both the digits through a hashing array, If they evaluate to the same, accept that digit. Atmost one hashing per code.

- Anonymous March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a Hashmap with key equals to the digit in the key pad. The values will be the allowable digits on keypad for that key. then lookup the hashmap for that particular key.

- Anonymous March 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a Hashmap with key equals to the digit in the key pad. The values will be the allowable digits on keypad for that key. then lookup the hashmap for that particular key.

- Anonymous March 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap is overkill and pointless, if you have to anyway go through the whole list of allowed digits for that particular key...

Just map digit to the row,colum.

bool CheckValidGuess(int securityDigit, int guess) {
    
    int srow = securityDigit/3 + 1;
    int scol = (securityDigit % 3);
    if (scol == 0) { scol = 3;}

    int grow, gcol = // similar to above

    if ( (grow-srow)*(grow-srow) + (gcol-scol)*(gcol-scol) <= 1) {
        return true;
    }
     
    return false;
}

- Anonymous March 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction to the code :

public static boolean  CheckValidGuess(int securityDigit, int guess) {

		 int srow ; 
		 srow = securityDigit/3 + 1;
		 if(securityDigit%3==0)srow = srow-1; 
			 
		int scol = (securityDigit % 3);
		if (scol == 0) { scol = 3;}
		
		System.out.println(srow);
		System.out.println(scol);
		
		int grow ; 
		 grow = guess/3 + 1;
		 if(guess%3==0)grow = grow -1;
	
		int gcol = (guess % 3);
		if (gcol == 0) { gcol = 3;}

		System.out.println(grow);
		System.out.println(gcol);
		
		
		if ( (grow-srow)*(grow-srow) + (gcol-scol)*(gcol-scol) <= 1) {
		//	System.out.println((grow-srow)*(grow-srow) + (gcol-scol)*(gcol-scol));			
			return true;
		}

		return false;
		}

- Rushabh Shah March 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean(int user_code,int enter_code)
{
int count=0;
boolean flag=true;
int temp1,temp2;

if(enter_code==user_code)
return true;
else
{
while(user_code!=0)
{
if(count<1)
{
temp1=user_code%10;
user_code = user_code/10;
temp2=enter_code%10;
user_code = enter_code/10;
if(temp2==0)
{
flag=false;
break;
}
if(temp1==temp2)
continue;
else
{
if(temp1==1 && (temp2 ==2 || temp2==4)///repeat for other digits
{
count++;
continue
}
}
else
{
flag = false;
break;
}
}
}
if(flag)
return true;
else
return false;
}

- suggest April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean(int user_code,int enter_code)
{
int count=0;
boolean flag=true;
int temp1,temp2;

if(enter_code==user_code)
return true;
else
{
while(user_code!=0)
{
if(count<1)
{
temp1=user_code%10;
user_code = user_code/10;
temp2=enter_code%10;
user_code = enter_code/10;
if(temp2==0)
{
flag=false;
break;
}
if(temp1==temp2)
continue;
else
{
if(temp1==1 && (temp2 ==2 || temp2==4)///repeat for other digits
{
count++;
continue
}
}
else
{
flag = false;
break;
}
}
}
if(flag)
return true;
else
return false;
}

- suggest April 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's just the relationship between numbers- An odd can have evens and itself and vice-versa.

- observer April 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the best and the fastest solution will be to check which one of the SecurityCode or EnteredCode is bigger and then get the modules result. This modules result can be checked against 5 values 0, -100, -300, 100 and 300. Value 0 to check if codes are same or else the modules result will always be one of -100, -300, 100 or 300, if there is a 1 digit error horizontal or vertical.

bool IsValidCode(int SecurityCode, int EnteredCode)
{
int ModResult = 0;

if (SecurityCode > EnteredCode)
ModResult = SecurityCode % EnteredCode;
else
ModResult = EnteredCode % SecurityCode;

if (ModResult == 0 || ModResult == -100 || ModResult == -300 ||
ModResult == 100 || ModResult == 300)
return true;
else
return false;
}

- Maddy April 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops sorry.. this solution will not work in all the cases. I think we will need to identify which digit is mismatching and repeat the same procedure for only that single digit. But in this case values to be checked against will be 0, -1, -3, 1 and 3. That should do the trick.

- Maddy April 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is it to
check difference=|SecurityCode - EnteredCode|
if difference=1000|| 2000|| 3000|| 6000 ||
100||200|| 300|| 600||
10|| 20||30|| 60||
1|| 2|| 3|| 6||

If yes, accept
otherwise, reject

- creation October 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this doesn't work
ie
4444 vs 4443

- emge October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@creation.. wat if the difference is 10000?
SecurityCode : 12345
EnteredCode : 22345
maddy is correct, checking the difference between each digit

- shivagarlapati November 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// loop for each charater in the actual
// for that character, if the corresponding entered character does not match,
// check if they are opposite with respect to add/even
// if yes, increment error count.
// return false if error count is > 1
// return true at the end of all character check

// security code checker
static bool IsValidSecurityCode(int actual, int entered)
{
char[] actualChars = actual.ToString().ToCharArray();
char[] enteredChars = entered.ToString().ToCharArray();
int errCount = 0;
for (int i = 0; i < actualChars.Length; i++)
{
if (actualChars[i] != enteredChars[i])
{
int first = int.Parse(actualChars[i].ToString());
int second = int.Parse(enteredChars[i].ToString());
if ((first % 2 == 0 & second % 2 != 0)
|| first % 2 != 0 & second % 2 == 0)
{
errCount++;
if (errCount > 1)
{
return false;
}
}
}
}
return true;

// test
Console.WriteLine("Codes 1478, 1178 match: {0}", IsValidSecurityCode(1123, 1143).ToString());

- Anonymous April 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But this code will return true for 1262 and 1762. Isnt this wrong?

- TxAxl August 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Change test line numbers:
Console.WriteLine("Codes 1478, 1178 match: {0}", IsValidSecurityCode(1478, 1178).ToString());

- Anonymous April 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean gird(int actual, int entered){
	 	String s1 = Integer.toString(actual);
	 	String s2 = Integer.toString(entered);
	 	int errorCount = 0;
	 	// check1: if both actual and entered length are not same
	 	
	 	if(s1.length() != s2.length()){
	 			return false;
	 		}
	 	// check2: If both the actual and entered are same 
	 	if(s1.equals(s2)){
	 			return true;
	 		}
	 	for(int i=0; i< s1.length(); i++){
	 		char c1 = s1.charAt(i);
	 		char c2 = s2.charAt(i);
	 		if(c1 != c2){
	 			int mod = c1 % c2;
	 			if(mod == -1 || mod == 1 || mod == 3 || mod == -3){
	 				errorCount++;
	 			}
	 			else{
	 				return false;
	 			}
	 		}
	 		if(errorCount > 1){
	 			return false;
	 		}
	 		
	 		
	 	}
	 	return true;
	}

- pondy April 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if C1 =7, C2=2, then it wont work..as u get 1 and it returns true...But 7 is not right for 2. Is it not?

- TxAxl August 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean validate(int valid,int entered){
int x,y,t1,t2,count=0,flag=0;
if(valid==entered){
System.out.println("Allow");
return true;
}
if(valid/entered >10 || valid/entered <1 ){
System.out.println("Do not Allow");
return false;
}
While(entered>0){
x=valid%10;
y=entered%10;
if(x!=y){
t1=x;t2=y;
count++;
}
if(count>1)
{
flag=1;
break;
}
entered=entered/10;
valid=valid/10;
}
if(flag==1)
{
System.out.println("Do not allow");
return false;
}
else if(((t1-3)>0 && (t1-3)==t2) ||((t1+3)==t2) ||((t1-1)>0 && (t1-1)==t2)||((t1+1)==t2)){
System.out.println("Allow");
return true;
}
else{
System.out.println("Do not allow");
return false;
}
}

- suggest April 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean validateEntry(String inserted,String realValue)
{
boolean valid = false;
if(inserted.equals(realValue))
{
valid = true;
}
else if(inserted.length() != realValue.length())
{
valid = false;
System.out.println("2");
}
else
{
int errorCounter = 0;
for (int i=0;i<realValue.length();i++)
{
System.out.println("3");
if(errorCounter>1)
{
i = realValue.length();
}
else
{
int c1 = Character.getNumericValue(realValue.charAt(i));
int c2 = Character.getNumericValue(inserted.charAt(i));
if(c1 != c2)
{
errorCounter++;
if(errorCounter == 1)
{
if( (c1/3) == (c2/3) || (c1%3) == (c2%3))
{
valid = true;
}
else
{
valid = false;
}
}else{
valid = false;
}
}

}


}
}
return valid;
}

- Rajibul Alam April 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* There is a security keypad at the entrance of a building. It has 9 numbers 1
* - 9 in a 3x3 matrix format. 1 2 3 4 5 6 7 8 9
*
* The security has decided to allow one digit error for a person but that digit
* should be horizontal or vertical. Example: for 5 the user is allowed to enter
* 2, 4, 6, 8 or for 4 the user is allowed to enter 1, 5, 7.
*
* IF the security code to enter is 1478 and if the user enters 1178 he should
* be allowed.
*
* Write a function to take security code from the user and print out if he
* should be allowed or not
*/
public class SecurityKeypad {

public final static char[][] matrix = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' }
};

public static boolean authenticate(String code, String userCode) {
boolean errorFound = false;
boolean valid = false;
if(code.equals(userCode)){
valid = true;
}else if(code.length() != userCode.length()){
valid = false;
}
else{
int codeLength = userCode.length();
for(int i=0; i<codeLength;i++){
if(code.charAt(i) != userCode.charAt(i)){
if(errorFound){
return false;
}
for(int m = 0; m<3;m++){
for(int n=0;n<3;n++){
if(matrix[m][n] == code.charAt(i)){

errorFound = true;
if(checkPrev(m,n,userCode.charAt(i), code.charAt(i))){
valid = true;
}
else if(checkNext(m,n,userCode.charAt(i), code.charAt(i))){
valid = true;
}
else if(checkUp(m,n,userCode.charAt(i), code.charAt(i))){
valid = true;
}
else if(checkDown(m,n,userCode.charAt(i), code.charAt(i))){
valid = true;
}

}
}
}

}
}
}
return valid;
}

private static boolean checkPrev(int y, int x, char u, char c){
int prev = x - 1;

if(prev >= 0
&& matrix[y][prev] == u){
return true;
}
return false;
}

private static boolean checkUp(int y, int x, char u, char c){
int up = y - 1;

if(up >= 0
&& matrix[up][x] == u){
return true;
}
return false;
}

private static boolean checkDown(int y, int x, char u, char c){
int down = x + 1;
if(down >= 0
&& matrix[down][x] == u){
return true;
}
return false;
}

private static boolean checkNext(int y, int x, char u, char c){
int next = x + 1;

if(next <=2
&& matrix[y][next] == u){
return true;
}
return false;
}

public static void main(String[] args) {
if(authenticate("1478", "1178")){
System.out.println("Success!");
}
else{
System.out.println("Failure!");
}
}

}

- tj May 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class General {
static int width = 3;
static int height = 3;
static int securityCode[][] = new int[width][height];
static String code = "1478";
static String user_entered_code = "1278";
static int diff = 0;
public static void main(String[] args) {
int local = 1;
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
securityCode[i][j] = local;
local++;
}
}
for(int i=0;i<code.length();i++){
if(diff == 2){
System.out.println("Error");
break;
}
neighbours(code.charAt(i),user_entered_code.charAt(i));
}
if(diff < 2){
System.out.println("Success");
}
}

public static void neighbours(int or,int ur){
if(Math.abs(or - ur) == 1 || Math.abs(or - ur) == 3){
diff++;
}
else if(or == ur){

}
else{
diff = 2;
}
}

- Anonymous July 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What should this return? 1262 and 1762. Me thinking it should say error. 2 and 7 are not horizontal or vertical right?

- TxAxl August 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdlib.h>
using namespace std;

bool valid(char c1, char c2)
{
    int cInt1 = c1 - '0';
    int cInt2 = c2 - '0';

    switch(cInt1)
    {
        case 1:
            if(cInt2 == 2 || cInt2 == 4)
                return true;
            else
                return false;
            break;
        case 2:
            if(cInt2 == 1 || cInt2 == 3 || cInt2 ==5)
                return true;
            else
                return false;
            break;
        case 3:
            if(cInt2 == 2 || cInt2 == 6)
                return true;
            else
                return false;
            break;
        case 4:
            if(cInt2 == 1 || cInt2 == 5 || cInt2 ==7)
                return true;
            else
                return false;
            break;
        case 5:
            if(cInt2 == 2 || cInt2 == 4 || cInt2 ==6 || cInt2 ==8)
                return true;
            else
                return false;
            break;
        case 6:
            if(cInt2 == 3 || cInt2 == 5 || cInt2 ==9)
                return true;
            else
                return false;
            break;
        case 7:
            if(cInt2 == 8 || cInt2 == 4)
                return true;
            else
                return false;
            break;
        case 8:
            if(cInt2 == 5 || cInt2 == 7 || cInt2 ==9)
                return true;
            else
                return false;
            break;
        case 9:
            if(cInt2 == 8 || cInt2 == 8)
                return true;
            else
                return false;
            break;
        default:
            return false;
    }
}
bool isValidSecurityCode(string originalCode, string enteredCode)
{
    if(originalCode.length() != enteredCode.length())
        return false;

    bool flag = true;
    for(int i=0; i<originalCode.length(); i++)
    {
        if(originalCode.at(i) != enteredCode.at(i))
        {
            if(flag)
            {
                if(!valid(originalCode.at(i), enteredCode.at(i)))
                    return false;
                flag = false;
            }
            else
                return false;
        }
    }
    return true;
}
int main(int argc, char** argv)
{
    if(isValidSecurityCode("12345","12345"))
        cout <<"Code is valid"<<endl;
    else
        cout <<"Code is not valid"<<endl;
    return (EXIT_SUCCESS);
}

- Anonymous November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

error count is missing... The question says one digit. If you add a error counter in the break statement... That should resolve the issue.

- Krish December 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package b;

public class OneDigitErrorKeypad {
	
	public static void main(String[] args){
		String input = "12246";
		if(checkPass(input)){
			System.out.println("Correct");
		}else{
			System.out.println("INcorrect");
		}
	}
	
	public static boolean checkPass(String pass){
		String correctPass = "12345";
		//check that both passwords are of same length
		if(pass.length() != correctPass.length()){
			return false;
		}
		
		
		for(int i=0; i< pass.length(); i++){
			if(pass.charAt(i) != correctPass.charAt(i)){
				if(!validate(pass.charAt(i), correctPass.charAt(i))){
					return false;
				}
			}
		}		
		return true;
	}
	
	public static boolean validate(char a, char c){
		char[][] keypad = {{'1','2','3'},
						   {'4','5','6'},
						   {'7','8','9'}
						  };
		int indexX = 0, indexY = 0;
		
		for(int i = 0 ; i < keypad.length; i++){
			for(int j = 0 ; j < keypad[i].length; j++){
				if(a == keypad[i][j]){
					indexX = i;
					indexY = j;
				}
			}
		}
		
		//check horizontally
		if(indexX == 0){
			if(c == keypad[indexX+1][indexY]){
				return true;
			}
		}else if(indexX == 1){
			if(c == keypad[indexX+1][indexY]|| c == keypad[indexX-1][indexY]){
				return true;
			}
		}else{
			if(c == keypad[indexX-1][indexY]){
				return true;
			}
		}
		//Check vertically
		if(indexY == 0){
			if(c == keypad[indexX][indexY+1]){
				return true;
			}
		}else if(indexY == 1){
			if(c == keypad[indexX][indexY+1]|| c == keypad[indexX][indexY-1]){
				return true;
			}
		}else{
			if(c == keypad[indexX][indexY-1]){
				return true;
			}
		}
		
		return false; 
	}
}

- Dr. Java April 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the best code EVER for this problem (Njoi n cheers!!!)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
   char code[10]={2,3,4,9};
   char str[10];
   int i=0;
   int x=0,y=0;
   int flag=0;
   printf("\nEnter the code: ");
   scanf("%s",str);
   
   while(i<4)
   {
            
        if(int(str[i]-48)==int(code[i])) //if matches
                  i++;
        else                             //if not
        {
            if(flag==1)
                flag=2;
            if(flag==0)
                flag=1;
             if(flag==2)
             {
                  printf("\nCODE REJECTED!");
                  getch();
                  exit(0);
             }
           x=int(code[i]); 
           y=int(str[i]-48);
           i++;
           //For top
           if(x-3>0 && y==x-3)
                 continue;         
           //For bottom
           else if(x+3<10 && y==x+3)
                 continue;
           //For right
           else if(x+1!=4 && x+1!=7 && x+1!=10 && y==x+1)
               continue;
           //For left
           else if(x-1!=6 && x-1!=3 && x-1!=0 && y==x-1)
              continue;
           else
           {
               printf("\nCODE REJECTED!");
                  getch();
                  exit(0);
           }
        }
   }
   
   if(flag==0 || flag==1)
      printf("\nCODE ACCEPTED!");
   getch();
}

- Sachin Tendulkar's devotee April 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey66621" class="run-this">#include <stdio.h>

int main (void)
{
int number;
int code[5]={5,7,3,4,9};
int a[3][3]={0};
int i,j,digit=1;
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
a[i][j]=digit++;
printf("%d\n", a[i][j]);
}
}

printf("Enter Number");
scanf("%d", &number);
int k=0;
// printf("%d\n", code[k]);
int flag;

while(number!=0)
{
flag=0;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d", a[i][j]);

if(a[i][j]==code[k])
{
if(number==code[k])
{
k++;
flag=1;
break;
flag=1;
}
else if(number==a[i][j+1]||number==a[i][j-1]||number==a[i+1][j]||number==a[i-1][j])
{
k++;
flag=1;
break;
}
else
{
printf("code doesnt match");
flag=1;
break;
}
}
}
if(flag==1)
{
break;
}
}

scanf("%d", &number);
}

return 0;
}


</pre><pre title="CodeMonkey66621" input="yes">
4
8
2
1
6
</pre>

- andaleef November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EQ_15
{
    class Program
    {

        static char[,] matrix = {
                                           {'1','2','3'},
                                           {'4','5','6'},
                                           { '7', '8', '9'}
                                       };

      

        public static bool authenticate(String code, String userCode)
        {
             bool valid = false;

            if (code == userCode)
            {               
                return true;
            }
            else if (code.Length != userCode.Length)
            {
                return false;
            }
            else
            {
                int codeLength = userCode.Length;
                for (int i = 0; i < codeLength; i++)
                {
                    for (int m = 0; m < 3; m++)
                    {
                        for (int n = 0; n < 3; n++)
                        {
                            if (matrix[m,n] == code[i])
                            {
                                if (checkPrev(m, n, userCode[i]))
                                { valid = true; }                              
                                else if (checkNext(m, n, userCode[i]))
                                { valid = true; }                              
                                else if(checkUp(m,n,userCode[i]))
                                {valid = true;}                              
                                else if (checkDown(m, n, userCode[i]))
                                { valid = true; }
                                //in a single interration when we get failures from all the methods we have to say its not a right code
                                if (!valid)
                                {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
            return valid;
        }

        public static bool checkPrev(int y, int x, char u)
        {
            int prev;

            if (x != 0) { prev = x - 1; }
            else { prev = x; }

            if (prev >= 0 && matrix[y, prev] == u)
            {
                return true;
            }
            return false;
        }

        public static bool checkNext(int y, int x, char u)
        {
            int next;
             if (x == 2)  { next = x;}
            else { next = x + 1; }

            if (next >= 0 && matrix[y, next] == u)
            {
                return true;
            }
            return false;
        }

        public static bool checkUp(int y, int x, char u)
        {
            int up;

            if (y == 2)
            { up = y; }
            else 
            { up = y + 1;}

            if (up >= 0 && matrix[up, x] == u)
            {
                return true;
            }
            return false;
        }

        public static bool checkDown(int y, int x, char u)
        {
            int down;
            if (y != 0) { down = y - 1; }
            else { down = y; }

            if (down >= 0 && matrix[down, x] == u)
            {
                return true;
            }
            return false;
        }

        static void Main(string[] args)
        {
            if (authenticate("1478", "1178"))
            {
                Console.WriteLine("Success!");
            }
            else
            {
                Console.WriteLine("Failure!");
            }
            Console.Read();
        }
    }
}

- CS_15 February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python:

def main():
	print passw(12345,22345)

def passw(inp,pas):
	dic = {1:[2,4], 2:[1,5,3], 3:[2,6], 4:[1,5,7], 5:[2,4,6,8], 6:[3,5,9], 7:[4,8], 8:[5,7,9], 9:[6,8]}
	exception = -1
	if len(str(inp))!=len(str(pas)):
		return "Access Denied"
	else:
		for i in range(0,len(str(inp))):
			if (str(inp)[i]!=str(pas)[i]):
				if exception==-1:
					if dic[int(str(pas)[i])].count(int(str(inp)[i])) > 0:
						exception = 1
					else:
						return "Access Denied"
				else:
					return "Access Denied"
		return "Access granted" 

if __name__ == '__main__':
	main()

- Vikas November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;
import java.util.Map;

public class SecurityKisok {

	public static void main(String[] args) {
		System.out.println(isAllowed("1478", "1178"));
	}

	static boolean isAllowed(String correctCode, String userCode) {
		Map<String, String[]> allowedCodes = new HashMap<String, String[]>();
		allowedCodes.put("1", new String[] { "2", "4" });
		allowedCodes.put("2", new String[] { "1", "3", "5" });
		allowedCodes.put("3", new String[] { "2", "6" });
		allowedCodes.put("4", new String[] { "1", "5", "7" });
		allowedCodes.put("5", new String[] { "2", "4", "6", "8" });
		allowedCodes.put("6", new String[] { "3", "5", "9" });
		allowedCodes.put("7", new String[] { "4", "8" });
		allowedCodes.put("8", new String[] { "5", "7", "9" });
		allowedCodes.put("9", new String[] { "6", "8" });

		if (correctCode.equals(userCode)) {
			return true;
		} else if (correctCode.length() != userCode.length()) {
			return false;
		} else {
			for (int i = 0; i < userCode.length(); i++) {
				String c = correctCode.charAt(i) + "";
				String u = userCode.charAt(i) + "";
				if (!c.equals(u)) {
					String[] allowedCode = allowedCodes.get(u);
					boolean isOk = false;
					for (String x : allowedCode) {
						if (x.equalsIgnoreCase(c)) {
							isOk = true;
						}
					}
					if (!isOk) {
						return false;
					}
				}

			}
		}
		return true;
	}
}

Output: true

- Solution September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>

int security(int o, int n)
{
        int temp_o, temp_n, flag = 0;
        while(o > 0 && n > 0)
        {
                temp_o = o%10;
                temp_n = n%10;
                if(temp_o != temp_n)
                {
                        if(((temp_o + 3 == temp_n)&&(temp_o + 3 < 10)) || ((temp_o - 3 == temp_n) && (temp_o - 3 > 0)) || ((temp_o +1 == temp_n)&&((temp_o + 1)%3!=1 )) || ((temp_o - 1 == temp_n)&&((temp_o-1)%3!=0)))
                        {
                                if(flag == 0)
                                        flag =1;
                                else
                                        return 0;
                        }
                        else
                        {
                                return 0;
                        }
                }
                o = o/10;
                n = n/10;       
        }
        if(n!=0 || o!=0)
                return 0;
        else
                return 1;
}
int main()
{
        int valid = security(1478, 1175);
        if(valid)
                printf("Authorized!!\n");
        else
                printf("Failed!!\n");
        return 0;
}

Any comment pls

- kkkk October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class KeypadError {

	static String[] errorMap = {"0","24","135","157","2468","359","48","579","68"};
	public static void main(String[] args) {
		String exp = "1478";
		String actual = "1468";
		System.out.println("Allowed: " + isMatch(exp,actual));
	}
	private static boolean isMatch(String exp, String actual) {
		int errorCount = 0;
		if(exp.length()!=actual.length()) return false;
		
		for(int i = 0; i<exp.length();++i){
			if(exp.charAt(i) != actual.charAt(i)){
				if(errorCount < 1){ 
					if(checkError(Integer.parseInt(String.valueOf(actual.charAt(i))), exp.charAt(i)))
						errorCount++;
				}
				else
					return false;
			}
			else
				continue;
		}
		return true;		
	}
	private static boolean checkError(int parseInt,char r) {
		
		for(char c: errorMap[parseInt].toCharArray()){
			if(r==c)
				return true;
		}
		return false;
	}

}

- crazykani November 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;

public class valid_pass {
public static void main(String args[])
{
	int[] pass={4,6,3,8};
	int[] user_pass={4,5,6,5};
	Boolean b=true;
	b=is_Valid(pass,user_pass);
	if(b)
		System.out.println("valid pass");
	else System.out.println("invalid pass");
}
public static boolean is_Valid(int[] pass,int[] user_pass){
	int[][] keys={{1,2,3},{4,5,6},{7,8,9}};
	int temp1 = 0,temp2=0,p,q;
	if(pass.length !=user_pass.length)
	return false;
	else{
		for(int i=0;i<pass.length;i++){
			if(pass[i]!=user_pass[i]){
				 p=pass[i];
				 q=user_pass[i];
			
				if(q<p){
					for(int j=0;j<3;j++){
						for(int k=0;k<3;k++){
							
							if (keys[j][k]==p){
								
								if(j-1>=0){
									 temp1=keys[j-1][k];
								
								}
								if(k-1>=0){
									 temp2=keys[j][k-1];	
									 
								}
								if(temp1 != q && temp2 != q) return false;
						} 
						}
					}
				}else if(q>p){
					for(int j=0;j<3;j++){
						for(int k=0;k<3;k++){
						
							if (keys[j][k]==p){
								
								if(j+1<=2){
									 temp1=keys[j+1][k];
								
								}
								if(k+1<=2){
									 temp2=keys[j][k+1];	
								
								}
								if(temp1 != q && temp2 != q) return false;
						} 
						}
					}
				}
			}
		}
		return true;
	}
	
}
}

- Davi_singh April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;

public class valid_pass {
public static void main(String args[])
{
	int[] pass={4,6,3,8};
	int[] user_pass={4,5,6,5};
	Boolean b=true;
	b=is_Valid(pass,user_pass);
	if(b)
		System.out.println("valid pass");
	else System.out.println("invalid pass");
}
public static boolean is_Valid(int[] pass,int[] user_pass){
	int[][] keys={{1,2,3},{4,5,6},{7,8,9}};
	int temp1 = 0,temp2=0,p,q;
	if(pass.length !=user_pass.length)
	return false;
	else{
		for(int i=0;i<pass.length;i++){
			if(pass[i]!=user_pass[i]){
				 p=pass[i];
				 q=user_pass[i];
			
				if(q<p){
					for(int j=0;j<3;j++){
						for(int k=0;k<3;k++){
							
							if (keys[j][k]==p){
								
								if(j-1>=0){
									 temp1=keys[j-1][k];
								
								}
								if(k-1>=0){
									 temp2=keys[j][k-1];	
									 
								}
								if(temp1 != q && temp2 != q) return false;
						} 
						}
					}
				}else if(q>p){
					for(int j=0;j<3;j++){
						for(int k=0;k<3;k++){
						
							if (keys[j][k]==p){
								
								if(j+1<=2){
									 temp1=keys[j+1][k];
								
								}
								if(k+1<=2){
									 temp2=keys[j][k+1];	
								
								}
								if(temp1 != q && temp2 != q) return false;
						} 
						}
					}
				}
			}
		}
		return true;
	}
	
}
}

- Davi_singh April 17, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More