Cadence Inc Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Please explain the question clearly....

- Rahul September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Rahul,

I have given example. I you want here is one more:
i/p: N=33
arr= {3,6,8}

o/p should be:
3,6,8,13,16,18,23,26,28,30,31,32,33

need to print all numbers which contains any digit from arr.

- Abhi September 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}

}

- Vishal September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

def foo(n, arr):
  """"print all integers less than 'n' which contain atleast one digit from arr"""

  for i in range(n):
    curr = i
    while curr:
      mod = curr % 10
      div = curr / 10
      if mod in arr:
        # atleast one digit matches, exit early
        print i
        break
      curr = div

- Anonymous September 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
                }
            }
        }

- allanchanly September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
                }
            }

            Console.WriteLine();

}

- allanchanly September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;

}
}

}

}

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

i/p: N=33
arr= {3,6,8}

o/p should be:
3,6,8,13,16,18,23,26,28,30,31,32,33

but where is 1 in the given array

- nithindextrous September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- vikassanmacs September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test{

static int arr[] = {2,3};


public static void main(String[] args){

int n = 20;

for(int i=0;i<arr.length;i++){

for(int j=0;j<=n;j++){

String str = "" + j;
if(str.contains(arr[i]+"")){
System.out.println(str);
}
}
}

}
}

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

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);
}

}

}

- Anonymous September 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}
}

- krishna September 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the solution is looking straight forward and can be solved in O(m) where m is the size of array. Write all the permutation of arr(m) less than 'N' .

- mrads October 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

}
}

}
}
}

- Abhishek October 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int mult=10;
	for(int i=0;i<n;i++)
	{
		if(i/mult>=mult)
			mult*=10;
		if(i%mult==2 || i%mult==3)
			printf("%d\t",i);
		else
		{
			if(i%mult>10)
			{
				int r=i%mult;
				while(r>0)
				{
					if(r%10==2 || r%10==3)
						printf("%d\t",i);
					r=r/10;
				}
			}
		}
	}

- Anonymous October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With Javascript and a regular expression:

function matches(N, arr) {
	var m = [];
	var re = new RegExp('[' + arr.join('') + ']');
	for (var n = 0; n <= N; n++) {
		if ((""+n).match(re) != null) m.push(n);
	}
	return m;
}

e.g.: matches(20, [2, 3]) returns: [2, 3, 12, 13, 20]

- Curtis Autery October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Ted October 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;

	}

}

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

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)

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

#include<stdio.h>
void main()
{
int arr[20];
int i,n,j,n1;
printf("\nenter the nm");
scanf("%d",&n);
printf("enter array ele ");
scanf("%d",&n1);
for(i=0;i<n1;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n1;j++)
{
if(i%10==arr[j])
printf("%d\t",i);
}

}

}

- NITINRATHI1992 December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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.

- Ajit September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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;
	}

}

- Anonymous September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried your code with the i/p array [3, 6, 8] and N= 33, did not produce the numbers 30,31,32,33

- Ria September 23, 2013 | Flag


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