Cadence Inc Interview Question
SDE1sCountry: India
Interview Type: In-Person
public class Main{
public static void main(String[] args){
int[] arr = {3, 6, 8};
int rem,num=0;
for(int i=0; i<=33; i++){
if(i<10){
if(find(arr, i))
System.out.println(i);
}
else {num=i;
while(num>0){
rem=num%10;
num=num/10;
if(find(arr, rem)){
System.out.println(i);
break;
}
}
}
}
}
static boolean find(int[] arr, int i){
for(int j=0; j<arr.length; j++){
if(i==arr[j])
return true;
}
return false;
}
}
a simply way to solve this, but there is another optimized algorithm to do it
public static void SimpleDo(int num, int[] digits)
{
for (int i = 0; i <= num; i++)
{
int num2 = i;
int digit = 0;
while (num2 > 0)
{
digit = num2 % 10;
foreach (int d in digits)
{
if (d == digit)
{
Console.Write("{0}\t", i);
}
}
num2 = num2 / 10;
}
}
}
import java.util.*;
public class CareerCup24September{
public static void main(String args[])
{
System.out.println("Enter the number");
int num = new Scanner(System.in).nextInt();
int [] arr = {1,2,3};
int i=0,n=0,rem=0;
HashSet<Integer> hs = new HashSet<Integer>();
for(i=0;i<arr.length;i++)
{
hs.add(arr[i]);
}
for(i=1;i<=num;i++)
{
n = i;
while(n>0)
{
rem = n%10;
if(hs.contains(rem))
{
System.out.println(i+" ");
break;
}
n = n/10;
}
}
}
}
#include<stdio.h>
int main()
{
int i,j;
int num2,rem;
int flag;
int arr[3]={1,2,3};
int num;
printf("\nEnter a number:");
scanf("%d",&num);
for(i=0;i<=num;i++)
{
num2=i;
flag=0;
while(num2>0)
{
rem=num2%10;
for(j=0;j<3;j++)
if(rem==arr[j])
{
printf("%3d",i);
flag=1;
}
if(flag)
break;
num2=num2/10;
}
}
return 0;
}
public class Find {
static int arr[] = {0,1};
static int N = 100;
static boolean findVal(int i){
int num = i;
int num1;
while(num != 0){
num1 = num %10;
num = num/10;
for(int k=0; k < arr.length;k++){
if(arr[k] == num1){
return true;
}
}
}
for(int k=0; k < arr.length;k++){
if(arr[k] == i){
return true;
}
}
return false;
}
public static void main(String args[]){
for(int i=0; i < N; i++){
if(findVal(i))
System.out.println(i);
}
}
}
public class Find {
static int arr[] = {0,1};
static int N = 100;
static boolean findVal(int i){
int num = i;
int num1;
while(num != 0){
num1 = num %10;
num = num/10;
for(int k=0; k < arr.length;k++){
if(arr[k] == num1){
return true;
}
}
}
for(int k=0; k < arr.length;k++){
if(arr[k] == i){
return true;
}
}
return false;
}
public static void main(String args[]){
for(int i=0; i < N; i++){
if(findVal(i))
System.out.println(i);
}
}
}
public static void printAll(int num,int[] numbers){
for(int i=1;i<=num;i++){
for(int arrI=0;arrI<numbers.length;arrI++){
//if its single digit
if(i<10){
//check is the number is equal to any inside the array
if(i==numbers[arrI]){
System.out.println("Found (<10) "+i);
continue;
}
}else{
//if the number is not sigle digit
int save=i;
int set=0;
while((save)!=0 && set==0){
set=0;
int d = save%10;
if(d==numbers[arrI]){
System.out.println("Found (>10) "+i);
set++;
}else{
save/=10;
}
}
}
}
}
}
My solution in Ruby:
def number(n, arr)
n_arr = []
i = 0
while i < n + 1
n_arr << i
i += 1
end
n_arr_str = n_arr.map {|x| x.to_s}
arr_str = arr.map {|x| x.to_s}
final_arr = []
i = 0
while i < n + 1
temp_arr = n_arr_str[i].chars
arr_str.each do |x|
if temp_arr.include?(x) == true
final_arr << n_arr[i]
end
end
i += 1
end
final_arr.uniq
end
public class Problem1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int range = 20; //Range
int[] arr = { 2, 3 };//Test values
printRange(range, arr);
}
public static void printRange(int rng, int[] arr) {
int x = 1;
int lenght = (rng + "").length();
System.out.println(lenght);
int upperLimit = (int) Math.pow(10, lenght - 1);
System.out.println("upperLimit " + upperLimit);
for (int i = 0; i < arr.length; i++) {
// if
// do and range check
x = 0;
while (x <= rng) {
if (contains(arr[i], x))
System.out.println(x);
x++;
}
}
}
public static boolean contains(int set, int superset) {
boolean ispresent = false;
char setchar = Character.forDigit(set, 10);
char[] arr = (superset + "").toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == (setchar)) {
ispresent = true;
break;
}
}
return ispresent;
}
}
Here is a solution in python (I think it works). Answers are not printed in order.
def occurences(n, array):
#Size is from 0 to 9
# array = [3, 6, 9]
error_counter = 0
for x in xrange(0, len(array)):
if (array[x] > n): error_counter += 1
index = x
copy = array[x]
x = copy
units = 10
while x <= n:
print x
x += units
array[index] *= 10;
if (error_counter < len(array)): occurences(n, array)
Sort the input arr .Check the length of N (let it be p) and extract every digit of N.Maintain p pointers and check that every pointer points to a value <= to its corresponding digit. traverse the last pointer through all elements such that the combination of all pointer <=N, if yes then print the number.
public class Main {
static int input[] = { 2, 3, 5 };
static int N = 14;
/**
* @param args
*/
public static void main(String[] args) {
int minimum = min();
for (int i = 0; i < minimum; i++) {
for (int j = 0; j < input.length; j++) {
int number = Integer.parseInt(i + "" + input[j]);
if (number <= N) {
System.out.println(number);
}
}
}
}
public static int min() {
int min = input[0];
for (int p : input) {
if (p < min) {
p = min;
}
}
return min;
}
}
Please explain the question clearly....
- Rahul September 23, 2013