Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Your code is right. It only fails when faulty number is next is at the end.
for example Instead of 18684 u get input as
a. 1868488 - result for this should be true
b. 1868466 - result for this should be false
with your code for both input you will get false. You just have to add extra check so additional code will be:
while(i<expected.length()&& expected.at(i)==faultyKey )
{
i++;
}
shouldn't the i and j equal to the string length, instead of length-1 at last checking?
Tester, how can you get 1868488 as the input??? since '8' is the key out of working in this example, which means this string would be regarded as "164" also.
The code does has problem. But it may not be as the tester said. It's true we can't get the input as 1868488 since the '8' is the not working key, however, we may have the expected as 186848 or 186847. Now when actual input is 164,
for 186848 as expected, the result should be right;
for 186847 as expected, the result should be wrong.
But the mindless's code will regard both as wrong.
And the modification is similar to tester's solution. The code is shown as follows.
public boolean checkValid(String actual, String expected){
char faultKey = '\0';
int i = 0, j = 0;
for(;i<actual.length() && j < expected.length();j++){
if(actual.charAt(i)!=expected.charAt(j)){
if(faultKey == '\0'){
faultKey = expected.charAt(j);
}else{
if(faultKey != expected.charAt(i))
return false;
}
i--;
}
i++;
}
while(j<expected.length() && expected.charAt(j) == faultKey)
j++;
return (i==actual.length() && j == expected.length())? true:false;
}
I think we need to consider the invalid input like actual = "1246" expected = "12646". We can check this by adding this condition
if(actual.charAt(i) != expected.charAt(j)) {
if(fault == '\0')
fault = expected.charAt(j);
else {
if(expected.charAt(j) != fault) return false;
}
--i;
}
// Extra check added
else{
if(fault != '\0' && actual.charAt(i) == fault) return false;
}
++i;
#include <stdio.h>
void check(int,int);
int main()
{
int a,b;
printf("this program works only for positive integers \n");
printf("enter expected passwd \n");
scanf("%d",&a);
printf("enter input passwd \n");
scanf("%d",&b);
check(a,b);
return 0;
}
void check(int op, int ip)
{
int flag_digit = 0;
int k1,k2,k3;
int faulty_digit;
while(op){
k1 = op%10;
k2 = ip%10;
k3 = 1;
if(k1==k2){
op = op/10;
ip = ip/10;
}
else{
if(!flag_digit){
faulty_digit = k1;
flag_digit++;
op=op/10;
k3++;
}
if(faulty_digit != k1){
printf("Incorrect password \n");
break;
}
if(faulty_digit == k1 && (k3 ==1)){
op = op/10;
}
}
}
I guess it wont work if
faulty key is 4.
expected password :- 145645
input password :- 1565
Here is a working code in C++. I have basically used hashing to keep a count of each digit that occurs in both the passwords. If I find that more then two digits vary in their hashed values then it is a wrong password. If no such condition occurs (i.e. only one digit difference is there) then I further go on to check the correct ordering of the digits within the number. Each time I come across the digit whose hashed value differed I simply skip it. If there is a mismatch and it is not that particular digit then it is a wrong password.
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long int temp2, flag=0, temp, num1, num2, arr1[10], arr2[10], i, dig;
cin>>num1;
cin>>num2;
for (i=0; i<10; i++)
{
arr1[i]=arr2[i]=0;
}
temp=num1;
while (temp!=0)
{
arr1[temp%10]++;
temp/=10;
}
temp=num2;
while (temp!=0)
{
arr2[temp%10]++;
temp/=10;
}
for (i=1; i<10; i++)
{
if ((arr1[i]!=arr2[i]) && flag==1)
{
cout<<"Wrong password"<<endl;
exit(0);
}
else if((arr1[i]!=arr2[i]) && flag==0)
{
dig=i;
flag=1;
}
}
temp=num1;
temp2=num2;
flag=0;
while (temp2!=0)
{
if (((temp2%10)!=(temp%10)) && (temp2%10!=dig))
{
cout<<"Wrong Password"<<endl;
flag=1;
break;
}
if (((temp2%10)!=(temp%10)) && (temp2%10==dig))
{
temp2/=10;
}
else
{
temp/=10;
temp2/=10;
}
}
if (flag==0)
{
cout<<"Right password"<<endl;
}
return 0;
}
static boolean isValidPassword(String act, String exp) {
if (act.length() == 0 && exp.length() == 0) {
return true;
}
if (act.length() == 0)
return false;
char notWorkingPad = '0';
int j=0;
for (int i = 0; i < exp.length(); i++) {
if (exp.charAt(i) != act.charAt(j)) {
if (notWorkingPad == '0') {
notWorkingPad = exp.charAt(i);
} else {
if (notWorkingPad != exp.charAt(i)) {
return false;
}
}
}
else{j++;}
}
return true;
}
Same issue with your code. if the wrong number is at the end of the actual string, then your code is wrong. You can just add a checker before your checking.
public class keypad
{
static boolean isValidPassword(String act, String exp)
{
if (act.length() == 0 && exp.length() == 0)
{
return true;
}
if (act.length() == 0)
return false;
char notWorkingPad = '0';
int j = 0;
for (int i = 0; i < exp.length(); i++)
{
if (j >= act.length())
{
if (notWorkingPad != exp.charAt(i))
{
return false;
}
}
if (exp.charAt(i) != act.charAt(j))
{
// get the not working pad
if (notWorkingPad == '0')
{
notWorkingPad = exp.charAt(i);
}
else
{
if (notWorkingPad != exp.charAt(i))
{
return false;
}
}
}
else
{
j++;
}
}
return true;
}
public static void main(String[] args)
{
System.out.println(isValidPassword("164", "164"));
System.out.println(isValidPassword("164", "1868888847665"));
}
}
#include <stdio.h>
#include <string.h>
int valid(char *pass, char *str, int lengthpass, int lengthstr, int value) {
int i = 0;
int j = 0;
for(i =0 ; i < lengthpass ;) {
if(pass[i] != str[j]) {
if(pass[i]-'0' == value) {
i++;
}
else {
return 0;
}
}
else {
i++;
j++;
}
}
return 1;
}
int main() {
char *str = "1565";
char *password = "145645";
if(valid(password, str, strlen(password), strlen(str), 4)) {
printf("valid\n");
}
else
printf("invalid\n");
return 0;
}
This code works fine! (I think)
string expected, ent;
bool pwdMatch(int i, int j, char &miss){
if(i==expected.size() && j==ent.size()) return true;
if(i==expected.size() && j<ent.size()) return false;
if(i<expected.size() && j==ent.size()){
if(miss=='x') miss=expected[i];
else if(expected[i]!=miss){ return false;}
return pwdMatch(i+1,j,miss);
}
if(expected[i]==ent[j]) return pwdMatch(i+1,j+1,miss);
if(expected[i] != ent[j] && miss=='x'){
miss=expected[i];
return pwdMatch(i+1,j,miss);
}
if(expected[i] != ent[j] && expected[i]==miss){
return pwdMatch(i+1,j,miss);
}
if(expected[i] != ent[j] && expected[i]!=miss){
return false;
}
}
int main(){
cin>>expected>>ent;
char miss='x';
if(pwdMatch(0,0,miss)) cout<<"correct match\n";
else cout<<"invalid password\n";
}
public boolean isMatch(String input, String expected) {
if(input==""&&expected=="")
return true;
if(input.length()>expected.length())
return false;
char missed = 0;
int i = 0;
int j = 0;
while(i<input.length()) {
if(input.charAt(i)==expected.charAt(j)) {
i++;
j++;
}else {
if(missed!=0&&expected.charAt(j)!=missed)
return false;
else {
missed = expected.charAt(j);
for(int k=0; k<=i; k++) {
if(input.charAt(k)==missed)
return false;
}
}
j++;
}
}
if(j==expected.length())
return true;
else {
for(int k=j; k<expected.length(); k++) {
if(expected.charAt(k)!=missed)
return false;
}
return true;
}
}
Python code.
def main():
expected_password=raw_input("Enter the expected password:")
actual_password=raw_input("Enter the actual Password:")
faulty_digit=[]
missmatch=[]
l=len(expected_password)
k=len(actual_password)
j,i=0,0
if k<=l:
while i<k and j<l:
if actual_password[i]==expected_password[j]:
j=j+1
i=i+1
else:
missmatch.append(j)
j=j+1
for i in missmatch:
faulty_digit+=expected_password[i]
z=len(set(faulty_digit))
if z!=1 and z>0:
print "Wrong Password"
else:
print "Password accepted"
else:
print "Wrong Password"
if __name__ == "__main__":
main()
#include<iostream>
using namespace std;
int main()
{
int arr[10],ent,exp,temp=0;
cin>>exp;
for(int i=0;i<10;i++)
arr[i]=0;
temp=exp;
while(temp)
{
arr[temp%10]++;
temp=temp/10;
}
cin>>ent;
temp=ent;
while(temp)
{
arr[temp%10]--;
temp=temp/10;
}
int count=0;
for(int i=0;i<10;i++)
{
if(arr[i]!=0)
count++;
}
if(count>1)
cout<<"Invalid";
else
cout<<"Valid";
return 0;
}
int enteredPwd = 164;
int expectedPassword = 18684;
int faultKey = 9;
int enteredNo = enteredPwd;
int expectedNo = expectedPassword;
if (enteredNo == expectedNo) {
System.out.println("Password Correct");
} else {
while (enteredNo > 0 && expectedNo > 0) {
int enteredRem = enteredNo % 10;
int enteredQuo = enteredNo / 10;
int expectedRem = expectedNo % 10;
int expectedQuo = expectedNo / 10;
if (enteredRem == expectedRem) {
enteredNo = enteredQuo;
expectedNo = expectedQuo;
} else {
if (expectedRem == faultKey) {
expectedNo = expectedQuo;
} else {
System.out.println("Password Wrong");
System.exit(0);
}
}
}
System.out.println("Password Correct");
}
this should be the right one :
check it out
public static void epic(int[][] m) {
if(m.length==0 || m[0].length==0)
return;
int[][] dp = new int[m.length][m[0].length];
int max= 0;
for(int i=1;i<m.length;i++){
for(int j=1;j<m[0].length;j++){
if(Math.abs(m[i-1][j]-m[i][j])==1){
dp[i][j] = Math.max(dp[i][j],dp[i-1][j]+1);
max = Math.max(max,dp[i][j]);
}
if(Math.abs(m[i][j-1]-m[i][j])==1){
dp[i][j] = Math.max(dp[i][j],dp[i][j-1]+1);
max = Math.max(max,dp[i][j]);
}
}
}
for(int i=0;i<m.length;i++){
for(int j=0;j<m[0].length;j++){
if(dp[i][j]==max){
printsnake(m,dp,i,j,new Stack<Integer>());
}
}
}
}
public static void printsnake(int[][] m,int[][] dp,int row,int col,Stack<Integer> stack){
if(dp[row][col] == 0){
System.out.print(" snake: "+m[row][col]+" ");
Stack<Integer> s = (Stack<Integer>)stack.clone();
while(!s.isEmpty()){
System.out.print(s.pop()+" ");
}
System.out.println();
}
stack.add(m[row][col]);
if(row>0 && dp[row-1][col]+1==dp[row][col]){
printsnake(m,dp,row-1,col,stack);
}
if(col>0 && dp[row][col-1] +1==dp[row][col])
printsnake(m,dp,row,col-1,stack);
stack.pop();
}
public class PasswordChecker
{
public static boolean isValidPassword(String input, String expected)
{
String temp = "";
// remove all possible characters once at a time
for (int i = (int) '1'; i <= (int) '9'; i++)
{
// Check if current character exists in expected pw and
// doesn’t exist in input
if (!input.contains(((char) i)+"") && expected.contains(((char) i)+""))
{
// remove all occurrences of character in expected
temp = expected.replaceAll(((char) i)+"", "");
// return true if it is the same as input
if (input.equals(temp))
return true;
}
}
return false;
}
public static void main(String[] args)
{
System.out.println(isValidPassword("1698324786", "15698532547856")); // true
System.out.println(isValidPassword("16983286", "15698532547856")); // false
}
}
I didn't see all the codes posted but went through for some of them. My idea is simple. Just store 1st non-matching character/number in both provided numbers. This number will be our fault number which can not be pressed. If we find any other non-matching number and that number is also not matching to previous fault number, then it means there are more than two fault numbers which can not be true. So return.
private static boolean isFaultyPasscode(char[] entered, char[] actual) {
if (actual == null || entered == null)
return true;
if (actual.length < entered.length)
return true;
char faultChar = '\0';
int i,j;
i=j=0;
while (i<actual.length && j<entered.length) {
if (entered[j] != actual[i]) {
if (faultChar == '\0')
faultChar = actual[i++];
else {
if (actual[i++] == faultChar)
continue;
else
return true;
}
}
else {
i++;
j++;
}
}
if (j != entered.length)
return true;
while (i<actual.length) {
if (actual[i++] != faultChar)
return true;
}
return false;
}
public static void main(String args[])
{
String entered = "1565";
String actual = "145645";
System.out.println("Is a faulty code entered?: "+isFaultyPasscode(entered.toCharArray(), actual.toCharArray()));
}
working
#include <string>
#include <iostream>
using namespace std;
bool validPwd(string entered, string expected) {
if (entered == expected) return true;
char faultKy = 'm';
int i = 0;
for(int j = 0; j < expected.length(); ++j) {
if(i < entered.length()){
if (expected[j] != entered[i]) {
if(faultKy != 'm' && faultKy != expected[j]) {
return false;
} else if(faultKy == 'm') {
faultKy = expected[j];
}
} else {
if(i == entered.length() - 1 && j == expected.length() - 1) {
return true;
} else {
++i;
}
}
} else {
if (faultKy != expected[j]) return false;
}
}
return true;
}
void main() {
bool tmp = validPwd("164","1868466");
cout << tmp <<endl;
cin.get();
}
import java.util.*;
public class validPassword{
public static void main(String args[]){
String actualPassword = "18864";
String enteredPassword ="164";
ArrayList<Integer> actualPasswordDigits = new ArrayList<Integer>();
ArrayList<Integer> enteredPasswordDigits = new ArrayList<Integer>();
int iActualPassword = Integer.parseInt(actualPassword);
int iEnteredPassword = Integer.parseInt(enteredPassword);
while(iActualPassword !=0){
int temp = iActualPassword%10;
if(!actualPasswordDigits.contains(temp)){
actualPasswordDigits.add(temp);
}
iActualPassword = iActualPassword/10;
}
while(iEnteredPassword !=0){
int temp = iEnteredPassword%10;
if(!enteredPasswordDigits.contains(temp)){
enteredPasswordDigits.add(temp);
}
iEnteredPassword = iEnteredPassword/10;
}
if(Math.abs(actualPasswordDigits.size() - enteredPasswordDigits.size()) > 1 ){
System.out.println("Invalid Password");
System.exit(0);
}
Collections.reverse(enteredPasswordDigits);
Collections.reverse(actualPasswordDigits);
for(int i = 0; i<actualPasswordDigits.size();i++){
if(enteredPasswordDigits.indexOf(actualPasswordDigits.get(i)) > i){
System.out.println("Invalid Password due to sequence");
System.exit(0);
}
}
System.out.println("Valid password");
}
}
What's the meaning of this question? If user enters 818684, while you expect 18684, should we still return true since the computer only sees 164? Anyway, if that's what EPIC wants, we can come up with the following two ideas.
* We can either eliminate all faulty chars in expected string and do string compare,
* or compare two strings char by char. If faulty chars are found in expected string, skip.
Playable code at:
ideone.com/DtlAK2
bool checker(string actual, string expected, char fault) {
if (actual.size() > expected.size()) return false;
int actPt(0), expPt(0);
while (actPt != actual.size() && expPt != expected.size()) {
if (expected[expPt] == fault) {
++expPt;
continue;
}
if (actual[actPt] != expected[expPt]) return false;
++actPt, ++expPt;
}
while (expPt != expected.size() && expected[expPt] == fault) ++expPt;
return actPt == actual.size() && expPt == expected.size();
}
public class ValidPassword {
public static boolean isValidPwd(String input, String expected){
if(input.equals(expected)) return true;
if(input.length()>expected.length()) return false;
char diff=expected.charAt(expected.length()-1);
for(int i=0;i<input.length();i++){
if(input.charAt(i)!=expected.charAt(i)){
diff=expected.charAt(i);
break;
}
}
String compare=expected.replace(String.valueOf(diff),"");
if(input.equals(compare)) return true;
else return false;
}
public static void main(String[] args){
System.out.println(isValidPwd("164","16477"));
}
}
public class ValidPassword {
public static boolean isValidPwd(String input, String expected){
if(input.equals(expected)) return true;
if(input.length()>expected.length()) return false;
char diff=expected.charAt(expected.length()-1);
for(int i=0;i<input.length();i++){
if(input.charAt(i)!=expected.charAt(i)){
diff=expected.charAt(i);
break;
}
}
String compare=expected.replace(String.valueOf(diff),"");
if(input.equals(compare)) return true;
else return false;
}
public static void main(String[] args){
System.out.println(isValidPwd("164","16477"));
}
}
public class Password_match
{
private static boolean isFaultyPasscode(int given, int pass)
{ char [] entered =(""+given).toCharArray();
char[] expected =(""+pass).toCharArray();
if (expected == null || entered == null)
return true;
if (entered.length == 0 || entered.length>expected.length)
return false;
char key_faulty = '\0';
int i,j;
j=0;
for(i=0; i<expected.length; i++)
{
if(i>j)
{
if(expected[i] == key_faulty)
{
return true;
}
}
else if (entered[j] != expected[i])
{
if (key_faulty == '\0')
{
key_faulty = expected[i];
}
else {
if (expected[i] == key_faulty)
{
i++;
continue;
}
else
return false;
}
}
else
{
j++;
}
}
return true;
}
public static void main(String args[])
{
int entered = 151;
int expected = 14514;
System.out.println("Entered password is valid ? "+isFaultyPasscode(entered, expected));
}
}
public class Password_match
{
private static boolean isFaultyPasscode(int given, int pass)
{ char [] entered =(""+given).toCharArray();
char[] expected =(""+pass).toCharArray();
if (expected == null || entered == null)
return true;
if (entered.length == 0 || entered.length>expected.length)
return false;
char key_faulty = '\0';
int i,j;
j=0;
for(i=0; i<expected.length; i++)
{
if(i>j)
{
if(expected[i] == key_faulty)
{
return true;
}
}
else if (entered[j] != expected[i])
{
if (key_faulty == '\0')
{
key_faulty = expected[i];
}
else {
if (expected[i] == key_faulty)
{
i++;
continue;
}
else
return false;
}
}
else
{
j++;
}
}
return true;
}
public static void main(String args[])
{
int entered = 151;
int expected = 14514;
System.out.println("Entered password is valid ? "+isFaultyPasscode(entered, expected));
}
}
package EPi;
public class Password_match
{
private static boolean isFaultyPasscode(int given, int pass)
{ char [] entered =(""+given).toCharArray();
char[] expected =(""+pass).toCharArray();
if (expected == null || entered == null)
return true;
if (entered.length == 0 || entered.length>expected.length)
return false;
char key_faulty = '\0';
int i,j;
j=0;
for(i=0; i<expected.length; i++)
{
if(i>j)
{
if(expected[i] == key_faulty)
{
return true;
}
}
else if (entered[j] != expected[i])
{
if (key_faulty == '\0')
{
key_faulty = expected[i];
}
else {
if (expected[i] == key_faulty)
{
i++;
continue;
}
else
return false;
}
}
else
{
j++;
}
}
return true;
}
public static void main(String args[])
{
int entered = 151;
int expected = 14514;
System.out.println("Entered password is valid ? "+isFaultyPasscode(entered, expected));
}
}
- mindless monk November 14, 2013