Epic Systems Interview Question
Software Engineer / DevelopersDude Excellent work!!!!! U are a genius, if you have answered this one in just 20min of the exam time.
Generate the number first using N. Its probably something like
for(int i = 1; i<10; i+2)
{
for(int j = 1; j<10^N; j+2)
{
generatenumber = i*10^N + j;
something-------
check the digits using another function... best i could come up with...
Please note:
1.strictly ascending
2. N digits
Solution:
if (N<= 0) printf( "No solution\n");
if (N == 10) return printf( "01234456789\n");
for (i1=0; i1< 10-N; i1++)
{
printf("%1d");
if (N<9)
for (i1=1; i1< 10-N; i1++)
{
printf("%1d");
if (N<8)
for (i1=1; i1< 10-N; i1++)
{
... ...
}
}
}
prinf("\n");
My mi
stakes:
Should be:
Solution:
if (N<= 0) printf( "No solution\n");
if (N == 10) return printf( "01234456789\n");
for (i1=0; i1< 10-N; i1++)
{
printf("%1d", i1);
if (N>1)
for (i2=i1+1; i2< 10-N; i2++)
{
printf("%1d", i2);
if (N>2)
for (i3=i2+1; i3< 10-N; i3++)
{
... ...
if (N>8)
for (i9=i8+1; i9< 10-N; i9++)
{
printf("%1d", i9);
}
... ...
}
}
}
prinf("\n");
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N;
printf("enter the length of the password ");
scanf("%d",&N);
int x,i,j,a;
//since the password should be desired it should always start with (10-N)th
//digit to satisfy the desired condition
for(x=1;x<=(10-N);x++)
{
//start from number greater tham the first digit
for(i=x;i<=(10-N);i++)
{
printf("%d",x);
//go N times adding 1 to the previous digit
for(a=1;a<N;a++)
{
printf("%d ",(i+a));
}
printf("\n");
}
}
}
// This code is written if the number of digits in the pass word are 4.... the code could be modified accordingly for more numbers in the password.
public class DesirableNumber
{
public static void main(String args[]){
int numberOfDigits = 4;
for(int i = 0;i<9;i++){
for(int k =0;k<9;k++){
for(int j =0;j<9;j++){
String number = i+""+j+"" +k;
if(number.length() < numberOfDigits){
FindDesirable(number);
}else{
break;
}
}
}
}
}
private static void FindDesirable(String number) {
char a = number.charAt(0);
char b = number.charAt(1);
char c = number.charAt(2);
if(a<b && b<c){
System.out.println(number);
}
}
}
AllDesirable ad = new AllDesirable();
StringBuffer output = new StringBuffer(Integer.parseInt(argv[1]));
for (int i = 0; i < Integer.parseInt(argv[1]); i++)
output.append('0');
ad.printMe(Integer.parseInt(argv[1]), 0, output, 0);
public class AllDesirable {
public void printMe(int n, int start, StringBuffer output, int level){
if (n == level){
System.out.println(output);
return;
}
for (int i = start; i <= 9; i++){
output.setCharAt(level, (char) (i + '0'));
printMe(n, i + 1, output, level + 1);
}
}
}
int N = 5;
int[] a = new int[N];
getDigit(0, 0, N, a);
public static void getDigit(int digit, int i, int length, int[] a)
{
if(digit==length-1)
for(int j=i;j<=9;j++)
{
a[digit] = j;
for(int num:a)
System.out.print(num);
System.out.println();
}
else
for(int j=i;j<=9;j++)
{
a[digit] = j;
getDigit(digit+1, j+1, length, a);
}
}
Goal: Find all K-combinations (in lexographic order) of 9 digits.
Pseudo Code:
//this function finds the next combination in lexographic order
Nextcomb(comb)
{
while (comb != Lastcomb) do
{
print(comb);
B = comb;
i = 0;
while (i < K) and (B[i+1] == B[i]+1) do
{
B[i] = i; // if there is no gap in elements, and the end has not reached, then B[i]=i;
i++;
}
B[i] = B[i]+1; // otherwise, jump by 1;
Nextcomb(B);
}
}
//Main function
Input = K; // length of password
Lastcomb = 9-K+1,9-K,...,9;
Firstcomb = 0,1,2,3,...,K-1;
Nextcomb(Firstcomb);
List<Integer> PasswordList(int startInt, int N)
{
List<Integer> finalList=new ArrayList<Integer>();
int R = 9-N+1;
if(N==1)
{
for(int t=startInt;t<=9;t++)
{
finalList.add(t);
}
return finalList;
}
for(int i=startInt;i<=R;i++)
{
List<Integer> prevList = PasswordList(i+1,N-1);
for(int j=0;j<prevList.size();j++)
{
Double temp = Math.pow(10,(N-1));
finalList.add(i*temp.intValue() + prevList.get(j));
}
}
return finalList;
}
public class Desire2 {
/**
* @param args
*/
public static void main(String[] args) {
desireNum("0",4);
}
public static void desireNum (String output,int level){
if(level==0){
System.out.print(Integer.parseInt(output)+" ,");
return;
}
for(int i=1;i<10;i++){
if(Integer.parseInt(output.charAt(output.length()-1)+"")<i){
//System.out.println(output.charAt(output.length()-1)+" "+i+" level"+level);
desireNum(output+i,level-1);
}
}
}
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int i, n, j, k, flag, lb, ub, x, y, cnt;
char c[10];
clrscr();
printf("\nEnter digits: ");
scanf("%d",&n);
lb=pow(10,n-1);
ub=pow(10,n);
cnt=0;
for(i=lb;i<ub;i++)
{
itoa(i,c,10);
flag=0;
for(j=0;j<n-1;j++)
{
x=c[j];
y=c[j+1];
if(x<y)
flag=flag+1;
}
if(flag==(n-1))
{ printf("\t");
cnt=cnt+1;
for(k=0;k<n;k++)
printf("%c",c[k]);
}
}
printf("\nTotal no. of desirable numbers having %d digits is: %d",n,cnt);
getch();
}
Here's Python:
def getDesirables(N):
if N < 1:
return None
if N is 1:
return [str(i) for i in range(10)]
desirables = []
for s in getDesirables(N-1):
last_digit = int(s[-1])
for t in range(last_digit+1,10):
desirables.append(s+str(t))
return desirables
print(getDesirables(0)) # N must be positive, else None
print(getDesirables(1))
print(getDesirables(2))
package b;
public class DesirableNum {
public static void main(String[] args){
int N = 3;
printDesirable("", 0, N);
}
public static void printDesirable(String op, int start, int N){
if(N == 0){
System.out.println(op);
return;
}
for(int i = start; i < 10; i++){
String initial = op;
op += i;
printDesirable(op, i+1, N-1);
op = initial;
}
}
}
<pre lang="c#" line="1" title="CodeMonkey35355" class="run-this"> class Combination
{
static void Main(string[] args)
{
Console.Write("Enter Length: ");
int length = Convert.ToInt32(Console.ReadLine());
Combinations(length, "", 0);
Console.ReadLine();
}
private static void Combinations(int length, string combination, int intIndex)
{
if (combination.Length == length)
{
Console.WriteLine(combination);
}
else if (combination.Length < length)
{
for (int i = intIndex; i < 10; i++)
{
string newCombination = combination + i.ToString();
Combinations(length, newCombination, ++intIndex);
}
}
}
}</pre>
// Good question for practice
// Here is pesudo code , very simple
str = string of n length
For(i = 1;i<9-N;i++)
{
str[0] = ‘0’+i;
Print_desire(str,1,N,i);
}
Print_desire(char *str,int i,int n,int k)
{
If(i>=N)
{
Return;
}
If(i==N-1)
{
Print str;
}
For(j = 2;j<9;j++)
If(j>i)
{
Str[i] = ‘0’+j;
Print_desire(str,i+1,n,j);
}
}
}
// let me know if it is wrong !
//sort the numbers . Since it would n digits only, so we could use count sort,
//then use the permutation and avoid if the number is repeating.
class Program
{
static void Main(string[] args)
{
int[] digits = { 1, 2, 4, 5, 7, 8};
bool[] used = new bool[digits.Length];
Permutation(digits, used, 0, digits.Length-2, "",-10);
Console.Read();
}
public static void Permutation(int[] digits, bool[] used, int count, int length, string result,int pre)
{
if (count == length && result != " ")
{
Console.WriteLine(result);
return;
}
for (int i = 0; i < digits.Length; ++i)
{
if (used[i]||pre > digits[i])
continue;
pre = digits[i];
used[i] = true;
Permutation(digits, used, count + 1, length, result +" "+ digits[i],pre);
used[i] = false;
}
}
}
#include<iostream>
using namespace std;
void listAllOrderedNumbers(int str[],int length,bool mask[]);
int main()
{
int str[5];
str[0]=-1;
int length=1;
bool mask[10];
for(int i=1;i<=9;i++)
mask[i]=false;
listAllOrderedNumbers(str,1,mask);
}
void listAllOrderedNumbers(int str[],int length,bool mask[])
{
if(length==5)
{
for(int j=1;j<5;j++)
cout<<str[j]<<" ";
cout<<endl;
}
else
{
for(int i=1;i<=9;i++)
{
if(mask[i]==true)
continue;
if(str[length-1]< i)
{
str[length]=i;
mask[i]=true;
listAllOrderedNumbers(str,length+1,mask);
mask[i]=false;
}
}
}
}
public void printNum(int digits)
{
genNums(0,digits,-1,"");
}
public void genNums(int idx, int digits, int lastNum, String temp)
{
if(temp.length() == digits)
{System.out.println(temp); return;};
for(int n=lastNum+1;n<=10-digits+idx;n++)
{
genNums(idx+1,digits,n,temp+String.valueOf(n));
}
}
public void printNum(int digits)
{
genNums(0,digits,-1,"");
}
public void genNums(int idx, int digits, int lastNum, String temp)
{
if(temp.length() == digits)
{System.out.println(temp); return;};
for(int n=lastNum+1;n<=10-digits+idx;n++)
{
genNums(idx+1,digits,n,temp+String.valueOf(n));
}
}
/*
* password.cpp
*
* Created on: Aug 13, 2012
*
*/
#include <stack>
#include <stdio.h>
using namespace std;
void pass(stack <int> a,int n)
{
if(n==0)
{
while(!a.empty())
{
printf("%d ",a.top());
a.pop();
}
printf("\n");
}
else{
for(int i = 0;i<a.top();i++)
{
a.push(i);
pass(a,n-1);
a.pop();
}
}
}
int main()
{
int n;
scanf("%d",&n);
stack <int> a;
a.push(9);
pass(a,n-1);
}
Like most of the other code, I am using tail recursion. When numDigits is 0, I will just print what I have so far in the "curr" argument. Otherwise I will construct the new "curr" argument by multiplying it by 10 and adding the next digit to it. My only "innovation" is the use of the ones-digit to determine the minimum digit I should start iterating from.
static void printAll(int numDigits, int curr) {
if(numDigits == 0) {
System.out.println(curr);
return;
}
int min = 1 + (curr > 0 ? curr%10 : 0);
for(int d=min; d<=9; d++) {
printAll(numDigits-1, curr*10 + d);
}
}
int permutation_without_repetition(int start,int size,int index,char* password)
{
int num_of_instance=0;
if(size == 0)
{
cout<<password<<endl;
return 1;
}
for(int i=start;i<=10-size;i++)
{
password[index]='0'+i;
num_of_instance += permutation_without_repetition(i+1,size-1,index+1,password);
}
return num_of_instance;
}
void password_prob(int size)
{
char* password;
if(size>0){
password = new char[size];
int count = permutation_without_repetition(0,size,0,password);
cout<<"No. of possible passwords: "<<count<<endl;
}
else
{
cout<<"Invalid input !";
}
}
According to the problem, I rarely find any of above solutions which gives correct output. can you please look into this? Comments and feedbacks are highly appreciatable. Thanks.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author dell
*/
public class Main2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main2().substringCombination("", 3, 0);
}
public void substringCombination(String prefix, int n, int k) {
if (n == 0) {
System.out.println(prefix);
return;
}
for (int i = k+1; i <=9; i++) {
substringCombination(prefix +i , n-1,i);
}
}
}
#include <stdio.h>
void print(int arr[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d->",arr[i]);
}
printf("\n");
}
void generatePass(int arr[],int n, int start, int index)
{
int i;
// base case:
if(index == n)
{
print(arr,n);
}
// processing
for(i=start;i<=9;i++)
{
arr[index] = i;
generatePass(arr,n,i+1,index+1);
}
}
int main(void) {
// your code goes here
int arr[3];
int n=3;
int start =1;
int index = 0;
generatePass(arr,n,start,index);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int valid(int n)
{
int temp = n / 100, first = n % 10, second = (n/10) % 10;
if(first <= second)
return 0;
while(temp > 0)
{
first = second;
second = temp % 10;
if(first <= second)
return 0;
temp = temp / 10;
}
return 1;
}
void ascending(int s, int e, int size)
{
int i =0;
for(i=s;i<=e;i++)
{
if(valid(i))
printf("%d\n",i);
}
}
int main()
{
int start = 1, end =1, i=0, size = 2;
for(i=1;i<size;i++)
start = start * 10;
end = start * 10 - 1;
ascending(start, end, size);
return 0;
}
void dig_gen(int begin,int level, int sz , const int max, char* passwd)
{
int i=begin;
int j=0;
//printf("B=%d,L=%d,sz=%d\n",i,level,sz);
if (level >= max)
{
for ( j=0;j<max;++j)
printf("%d",passwd[j]);
printf(",");
return ;
}
for (i = begin; i<= 10-sz;++i)
{
passwd[level]=i;
//printf("%d",i);
dig_gen(i+1,level+1,sz-1,max,passwd);
}
}
void main()
{
char* passwd;
//scanf("%d",&size);
int size=3;
printf("start\n");
passwd=malloc(sizeof(char)*size);
dig_gen(0,0,size,size,passwd);
free(passwd);
}
#include <bits/stdc++.h>
using namespace std;
void findDesirable(int start, string passwd, int size)
{
if(size == 0)
cout << passwd << endl;
else
{
for(int i = start; i <= 9; i++)
{
string str = passwd + to_string(i);
findDesirable(i + 1, str , size - 1);
}
}
}
int main()
{
string str;
int size = 3;
findDesirable(0, str, size);
}
int size;
- recursion-andrewzp February 08, 2009void dig_gen(int m,int n, char* passwd)
{
int i,j;
if(n==size-1)
{
for(i=m;i<=9;i++)
{
passwd[n]='0'+i;
for(j=0; j<size; j++)
printf("%c",passwd[j]);
printf("\n");
}
}
else
{
for(i=m;i<10-size+n+1;i++)
{
passwd[n]='0'+i;
dig_gen(i+1,n+1,passwd);
}
}
}
void main()
{
char* passwd;
scanf("%d",&size);
passwd=malloc(sizeof(char)*size);
dig_gen(0,0,passwd);
free(passwd);
}