Epic Systems Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Written Test




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

<pre lang="" line="1" title="CodeMonkey99618" class="run-this">/* C# implementation */
public class UniqueFourDigits
{
public static void Display()
{
for (int currentNumber = 1000; currentNumber <= 9999; currentNumber++)
{
if (ContainsUniqueNumbers(currentNumber))
Console.WriteLine(currentNumber);
}
}

private static bool ContainsUniqueNumbers(int number)
{
bool status = true;
char[] stringNumber = number.ToString().ToCharArray();
for(int counter=0; counter < 3; counter++)
{
if(stringNumber[counter] == stringNumber[counter+1])
status = false;
}
if (status && stringNumber[0] == '4')
{
if (stringNumber[3] != '4')
{
status = false;
}
}
return status ;
}
}</pre><pre title="CodeMonkey99618" input="yes">
</pre>

- rajeshkumar.a November 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

best one.

- testPrep December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

best one.

- testPrep December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

counter <= 3 or counter < 4

also counter + 1 will give a out of bound exception. this might work better:

char temp = ' ';
 for(int counter=1; counter < 4; counter++)
            {
                temp = stringNumber[counter -1];
                if(temp == stringNumber[counter+1])
                    status = false;
            }

- Anonymous December 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

corrected code:
char temp = ' ';
for(int counter=1; counter < 4; counter++)
{
temp = stringNumber[counter -1];
if(temp == stringNumber[counter])
status = false;
}

- Anonymous December 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

it's more reasonable to put the examination for the leading and ending '4' before looping over the whole string.

- Suz February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

public class Digits {
	private int MAX;
	private int[] digits;

	private void print() {
		for (int i = 0; i < digits.length; i++)
			System.out.print(digits[i]);
		System.out.println();
	}

	private void generate(int index) {
		if (index == MAX)
			print();
		else {
			if (digits[0] == 4 && index == MAX - 1) {
				digits[index] = 4;
				generate(index + 1);
			} else {
				for (int i = 0; i < 10; i++) {
					if (index == 0 || digits[index - 1] != i) {
						digits[index] = i;
						generate(index + 1);
					}
				}
			}
		}
	}

	public void printDigits(int max) {
		this.MAX = max;
		this.digits = new int[MAX];
		generate(0);
	}

	public static void main(String[] args) {
		new Digits().printDigits(4);
	}

}

- Anonymous December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

best one

- janicewang03 January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one's time complexity is better than simply traversing all the numbers.

- NathanLRF January 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Recursive C# implementation that can handle a variable number of digits and
// skips ranges of numbers that don't meet the criteria
//
// Disadvantage of additional string creation
//
public static class UniqueDigits
{
    public static void Display(int numDigits)
    {
        if (numDigits < 2)
        {
            throw new ArgumentOutOfRangeException(
                "numDigits",
                "Display requires numDigits to be greater than 1.");
        }

        for (int i = 1; i < 10; i++)
        {
            ExpandDigits(i.ToString(), i, 1, numDigits, i == 4);
        }
    }

    private void ExpandDigits(string currentList,
                              int previousNumber,
                              int digitIndex,
                              int numDigits,
                              bool startsWithFour)
    {
        bool isLastDigit = digitIndex == numDigits - 1;
        bool isSecondToLastDigit = digitIndex == numDigits - 2;
    
        if (isLastDigit && startsWithFour)
        {
            Console.WriteLine(String.Concat(currentList, "4"));
        }
        else
        {
            for (int i = 0; i < 10; i++)
            {
                if ((i == previousNumber) || (isSecondToLastDigit && i == 4))
                {
                    continue;
                }

                string number = String.Concat(currentList, i.ToString());

                if (isLastDigit)
                {
                    Console.WriteLine(number);
                }
                else
                {
                    ExpandDigits(number, i, digitIndex + 1, numDigits, startsWithFour);
                }
           }
       }
    }
}

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

nothing fancy. made it for integers of size 3 instead of 4.

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

<pre lang="" line="1" title="CodeMonkey12882" class="run-this">public class cc1
{
public static void main (String args[])
{
int i=1,j=1,k=1;
for(i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
if(j==i)
continue;
else
{
for(k=1;k<10;k++)
{
if(j==k)
continue;
else
{
if(i==4)
System.out.println(i+" "+j+" "+4);
else
{
System.out.println(i+" "+j+" "+k);
}
}
}
}
}
}
}
}</pre><pre title="CodeMonkey12882" input="yes">
</pre>

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

#include<stdio.h>

int check(int x) {

int i,num[4],t,flag=1;
for(i=3;i>=0;i--)
{
num[i]=x%10;
x=x/10;
}//end for

for(i=0;i<3;i++){
if (num[i]==num[i+1]) 
     flag=-1;

}//end for

if(num[0]==4 && num[3]!=4)
     flag=-1;

return flag;

}



int main()
{
//printf("hello world");
int i,num=1000,flag;

while(num<=9999)
{
    flag=check(num);
    if(flag==1) { printf("%d\n",num); }
    num++;  
}



return 0;
}

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

#include<iostream.h>
int check(int x)
{
int i,num[4],t,flag=1;
for(i=0;i<3;i++){
if (num[i]==num[i+1])
flag=-1;
}
if(num[0]==4 && num[3]!=4)
flag=-1;
return flag;
}
void main()
{
int i,num=1000,flag;
while(num<=9999)
{
flag=check(num);
if(flag==1)
{
cout<<num;
}
num++;
}
}

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

public class Digits {
	private int MAX;
	private int[] digits;
	private void print() {
		for (int i = 0; i < digits.length; i++)
			System.out.print(digits[i]);
		System.out.println();
	}
	private void generate(int index) {
		if (index == MAX)
			print();
		else {
			if (digits[0] == 4 && index == MAX - 1) {
				digits[index] = 4;
				generate(index + 1);
			} else {
				for (int i = 0; i < 10; i++) {
					if (index == 0 || digits[index - 1] != i) {
						digits[index] = i;
						generate(index + 1);
					}
				}
			}
		}
	}
	public void printDigits(int max) {
		this.MAX = max;
		this.digits = new int[MAX];
		generate(0);
	}
	public static void main(String[] args) {
		new Digits().printDigits(4);
	}
}

- jiex.cs December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main(void){
int i = 0;
int temp = 0;
int dig1, dig2, dig3, dig4;
for( i = 1010; i <= 9898; i++){
temp = i;
dig1 = temp / 1000;
temp = temp % 1000;
dig2 = temp / 100;
temp = temp % 100;
dig3 = temp / 10;
dig4 = temp % 10;

if ( (dig1 == 4 && dig4 == 4 && dig2 != dig3 && dig2 != 4) ||
(dig1 != 4 && dig1 != dig2 && dig2 != dig3 && dig3 != dig4) )
printf("%-10d", i);
else // not necessary to write else continue;
continue;
}
}

- Anonymous January 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void num(string s){
  if(s.length() < 4){
    if (s.length() == 3 && s[0] == '4')
      num(s + '4');
    else{
      for(char i = '0' ; i <= '9' ; ++i){
	if(s[s.length() - 1] != i)
	  num(s+ i);
      }
    }
  }
  else{
    cout << s << endl ;
  }
}

int main(int argc, char **argv){
  string s;
  for(char i = '1' ; i <= '9' ; ++i)
    num(s + i );
}

- Anonymous February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*Generate all possible unique 4 digit numbers such that 
no two adjacent numbers are the same and any number starting 
with 4 should end with a 4 . eg 1234 , 1232 are both correct but 1223 is not .*/

public class FourDigit {


public static void main(String abc[]){
	int count=0;
	for(int i=1000;i<=9999;i++){
		int number = i;
		int firstdigit = number/1000;
		int seconddigit = (number-(firstdigit*1000))/100;
		int thirddigit = (number-(firstdigit*1000)-(seconddigit*100))/10;
		int fourthdigit = (number-(firstdigit*1000)-(seconddigit*100)-(thirddigit*10))%10;
			if((firstdigit==4)&&(fourthdigit==4)){
				if((firstdigit!=seconddigit) && (seconddigit!=thirddigit) && (thirddigit!=fourthdigit))
					System.out.println(i);
					count++;
					}
			else{
					if((firstdigit!=seconddigit) && (seconddigit!=thirddigit) && (thirddigit!=fourthdigit))
						System.out.println(i);
								count++;
			}


	}
				System.out.println("count is "+count);
}


}

- Sreeprasad April 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Works for not just 4 but for any other number of digits.

#include <iostream>
using namespace std;

void Generate (int& val, int curr, int dig, bool four) {

	if (curr == dig) {
		cout << val << "\t";
		return;
	}

	if (four == true && (curr == dig - 1)) {
		if (val % 10 != 4) cout << val * 10 + 4 << "\t";

		return;
	} 

	for (int i = 1; i <= 9; i++) {
		if (val % 10 != i) {
			val = val * 10 + i;
			Generate (val, curr + 1, dig, four);
			val = val / 10;
		}
	}
}

int main ( ) {
	int dig, val;
	cout << "Enter the number of digits: ";
	cin >> dig;

	for (int i = 1; i <= 9; i++) {
		if (i == 4) Generate (i, 1, dig, true);
		else Generate (i, 1, dig, false);
	}

	return 0;
}

- ranechabria July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * 
 */

/**
 * @author apoorvagarwal
 *
 */
public class PrintDigits {

	/**
	 * @param args
	 */
	static void makeNumber(int a, int b, int c,int d){
		System.out.println(""+a+b+c+d);
	}
	
	static void makeNumber(int focus,int length,String number){

		if(length == 4 &&  number.charAt(0) == '0')
			return;
		
		if(length == 4 &&  number.charAt(0) == '0')
			return;
		
		if(length == 4 &&  number.charAt(0) == '4' && number.charAt(3) != '4'){
			//System.out.println(""+number);
			return;
		}
		if (length == 4 && (number.charAt(0)!='0')){
			System.out.println(""+ number);
			return;
		}
		
		else{
			for(int i = 0;i<=9;i++){
				//System.out.println(""+ number +"     "+ focus);
				if(number.length()>0 && (number.charAt(focus-1)-48 == i))
					continue;
				number = number + i;
				makeNumber(focus+1, length+1, number);
				number = number.substring(0, focus);
			}
		}
	}
	
	
	public static void main(String[] args) {
		makeNumber(0,0,"");
	}

}

- Apoorv October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void GenerateNum()
{
	char d[4];
	int factor = 1000;
	int temp = 0;
	for(int num = 1000; num < 9999; num++)
	{
		int temp = num;
		for(int j = 3; j >= 0; j--)
		{
			d[j] = temp % 10;
			temp /= 10;
		}

		if(d[0] == 4 && d[3] != 4) continue;
		if(d[0] == d[1] || d[1] = d[2] || d[2] == d[3]) continue;
		
		printf("%d", num);
		
	}
}

- epicprepare October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{
	int i;
	for(i=1000;i<10000;i++)
		if(check(i))
			printf("%d\n", i);			
	return 0;
}

int check(int a)
{
	int d1, d2,d3, d4;
	d1 = a/1000;
	d2 = (a/100)%10;
	d3 = (a/10)%10;
	d4 = a%10;
	
	if(d1 == 4 && d4 != 4)
		return 0;
	else if(d1 != d2 && d2 != d3 && d3 != d4)
		return 1;
	else
		return 0;
}

- AbhinavChdhry January 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FourDigitUniqueNumber 
{
	
	public static int i, j, k =0;
	public static String temp;

	public static void main(String[] args) 
	{
		
		for(i=1230;i<10000;i++)
		{
			
			temp = String.valueOf(i);
			j = (int)(temp.charAt(0))-48;
			k = (int)(temp.charAt(3))-48;
			if((temp.charAt(0)!= temp.charAt(1)) && (temp.charAt(1)!= temp.charAt(2)) && (temp.charAt(2)!= temp.charAt(3)))
			{
				if(j!=4)
					System.out.println(temp);
				else if(k==4)
					System.out.println(temp);
			}
		
		}
		
	}

}

- GKR March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void genNum(int dig, char *c, int pos, int len)
{
	if (pos == len)
	{
		int p = 1;
		
		c[pos] = '\0';
		if (c[0] == '4')
		{
			if (c[len-1] != '4')
				p = 0;
		}
		if (p)
			printf("\n%s", c);
		return;
	}
	else
	{
		int i;
		for (i=0;i<=9;i++)
		{
			if (i != dig) {
			//printf("i=%d", i);
			c[pos] = i+'0';
			genNum(i, c, pos+1, len);
			}
		}
	}
}

int main()
{
	char c[100];
	
	genNum(0, c, 0, 3);
}

- C implementation August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a working code in C++. I have considered that a 4-digit number cannot start with 0. Though I have considered it, we can remove this restriction by deleting a few lines of code.

#include<cstdio>
#include<iostream>

using namespace std;

int func(int * arr,int *brr, int st,int ct, int prev);
int func(int * arr,int *brr, int st,int ct, int prev)
{
    int i;
    if (ct==2)
    {
        cout<<4;
        for (i=0; i<2; i++)
        {
            cout<<brr[i];
        }
        cout<<4<<endl;
    }
    else
    {
        for (i=0; i<9; i++)
        {
            if (arr[i]==prev || (ct==0 && arr[i]==0))
            {
                continue;
            }
            brr[ct]=arr[i];
            func(arr, brr, i+1, ct+1, arr[i]);
        }
    }
    return 1;
}

int hello(int * arr1,int *brr1, int st1,int ct1, int prev1);
int hello(int * arr1,int *brr1, int st1,int ct1, int prev1)
{
    int i;
    if (ct1==4)
    {
        for (i=0; i<4; i++)
        {
            cout<<brr1[i];
        }
        cout<<endl;
    }
    else
    {
        for (i=0; i<9; i++)
        {
            if ((ct1==0 && arr1[i]==4) || (arr1[i]==0 && ct1==0))
            {
                continue;
            }
            if (arr1[i]==prev1)
            {
                continue;
            }
            brr1[ct1]=arr1[i];
            hello(arr1, brr1, i+1, ct1+1, arr1[i]);
        }
    }
    return 1;
}


int main()
{
    int arr[]={1,2,3,5,6,7,8,9,0}, brr[2], brr1[4], arr1[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    func(arr, brr, 0, 0, -1);
    cout<<"next"<<endl;
    hello(arr1, brr1, 0, 0, -1);
    return 0;
}

- Meraj Ahmed November 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

print(0, 0, 4, 1, false, true);



private static void print(int number, int prev, int n, int start, boolean starts4, boolean firstTime) {

		if(n==0){
		System.out.println(number);
		return;
		}
		for(int i=start; i<=9; i++){
			if (firstTime && i == 4) {
				print(number*10 + i, i, n-1, 0, true, false);
			} else {
				if ((n - 1) == 0 && starts4) {
					if (prev != i && i == 4)
						print(number*10 + i, i, n-1, 0, starts4, false);
				} else if (prev != i)
				print(number*10 + i, i, n-1, 0, starts4, false);
			}
		}

	}

- Senthil Nathan Chockalingam February 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PrintNumbersWithNoAdjacentSame(int subNum, int index, int numberLength)
{
	if (numberLength == 0)
	{
		// skip number is it starts with 4 but doesn't end with 4
		if ((subNum > 4000 && subNum < 5000) && subNum % 10 != 4)
			return;
		cout << subNum << ", ";
	}
	else
	{
		for (int i = 1; i < 10; ++i)
		{
			// Make sure no two adjacent numbers are same
			if (subNum % 10 != i)
				PrintNumbersWithNoAdjacentSame(subNum*10 + i, i + 1, numberLength - 1); 
		}
	}
}

void PrintNumbersWithNoAdjacentSame()
{
	int numberLength;
	cin >> numberLength;

	PrintNumbersWithNoAdjacentSame(0, 1, numberLength);
}

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

def valid(number):
isValid = True
if number[0] == '4' and number[3] != '4':
isValid = False
for i in range(len(number)-1):
if number[i] == number[i+1]:
isValid = False
return isValid
def gen():
for i in range(10000):
out = str(i)
if len(out) < 4:
out = '0'*(4-len(out)) + out
if valid(out):
print out
gen()

- Anonymous August 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

9*8+8*9*9*9

- sanjay.2812 November 23, 2011 | 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