Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 3 vote

My solution is:

#include <iostream>

using namespace std;

static void printa(const int A[], int n) {
        for (int i = 0; i < n; ++i) {
                cout << A[i] ;
        }
        cout << endl;
}

static void p(int n, int A[], int i) {
        if (i == n) {
                printa(A, n);
                return ;
        }

        for (int k = 1; k <= n; ++k) {
                A[i] = k;
                p(n, A, i+1);
        }
}

void list_all(int n) {
        int A[n];
        p(n, A, 0);
}

int main() {
        list_all(10);
        return 0;
}

- blackball February 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

// assume N<=9 (just for easier printing, not harder otherwise)
void rassclot()
{
	static char ans[N+1] = {0};
	static int i=0;
	if(i==9) { puts(ans); return; }
	for(int j=0; i<N; j++) { 
		ans[i]='0'+j;  
		i++,  rassclot(), i--; 
	}
}

- SENBOTDRON the Transformer from Autobots actually dinobots February 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

or adjust the j-forloop to go from 1 to N or 0 to N (whatever the digits are)

- SENBOTDRON the Transformer from Autobots actually dinobots February 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#python code
def _all_numbers_recursive(n,depth,num = 0):

if depth == 0:

print num

return

for i in range(1,n + 1):

new_num = num + ((10**(depth - 1)) * i )

all_numbers_recursive(n, depth - 1, new_num)



def all_numbers_recursive(n):

_all_numbers_recursive(n,n)

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

function find_all_combinations(n, buf) {
	for (var i=1;i<=n;i++) {	
		if ( buf.length === n-1 ) {
			buf.push(i);
			console.log(buf.join(""));
			buf.pop();
		} else {
			buf.push(i);
			find_all_combinations(n,buf);
			buf.pop();
		}
	}
}

find_all_combinations(3,[]);

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

I think the best solution is to depict the problem as traversing n-ary tree with digits on its edges. When we reach the leaf we simply print accumulated value.

Here is code for my solution:

#include <cstdio>

int N;

void solution(int depth, int value)
{
	if (depth == N)
		printf("%d\n", value);
	else {
		for (int i=1; i<=N; ++i)
			solution(depth+1, value*10+i);
	}
}

int main()
{
	scanf("%d", &N);
	solution(0,0);

	return 0;
}

Not sure how to cope with N higher than 9 but it depends on tasks constraints.

- pawel.janus@student.uw.edu.pl February 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will work please try this:-

public class Sequence {

	
	 static void printAllKLength(char set[], int k) {
        int n = set.length;        
        printAllKLengthRec(set, "", n, k);
    }
 
    static void printAllKLengthRec(char set[], String prefix, int n, int k) {
         
        if (k == 0) {
            System.out.println(prefix);
            return;
        }
 
        for (int i = 0; i < n; ++i) {
             
            // Next character of input added
            String newPrefix = prefix + set[i]; 
             
            // k is decreased, because we have added a new character
            printAllKLengthRec(set, newPrefix, n, k - 1); 
        }
    }
	
	
	public static void main(String []args)
		{
		  Scanner scanner = new Scanner(System.in);
		  int n = scanner.nextInt();
		  String str = new String();
		  for(int i=1; i<=n; i++)
		  {
			 str=str+i; 
		  }
		  printAllKLength(str.toCharArray(),n);
			
		}

}

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

void print(int no, int digit, const int& k){
	if (digit == k){
		cout << no << endl;
	}
	else{
		for (int i = 1; i <= k; i++){
			print(no * 10 + i, digit + 1, k);
		}
	}
}
int main(){
	print(0, 0, 3);
}

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

public static void main(String args[]) {

String s = "6";

// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
int number = Integer.parseInt(s);
int i = 0;
int n = 0;

String trial = "";

for (int j = 0; j < number; j++) {
trial = trial + "1";
}
System.out.println("TRIAL:::"+trial);


String maxs= "";
for (int j = 0; j < number; j++) {
maxs = maxs + "9";
}
System.out.println("Max:::"+maxs);
if (number > 0) {
i = Integer.parseInt(trial);
n = Integer.parseInt(maxs);
}
for (; i <= n; i++) {
System.out.print(i);
System.out.println(",");
}

}

- SK SENTHILKUMAR February 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void print(int n) {
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = 1;
		}
		int k =1;
		//get k=n^n for total rows
		for (int j = 1; j <= n; j++) {
			k = k * n;
		}
		for(int x = 1;x<=k;x++){
			for(int i=0;i<n;i++){
				System.out.print(a[i]);
			}
			a[a.length-1]++;
			for(int z=a.length-1;z>0;z--){
				if(a[z]>n){
					a[z-1]++;
					a[z]=a[z]%n==0?n:a[z]%n;
				}
			}
			
			System.out.println();
		}
	}

- piaoyonghui March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.careercup.jenny;

import java.util.ArrayList;

public class Permutation {

public static void main(String[] args) {

int i =3;

ArrayList <StringBuffer> myList = getCombination(i, i, null);
for(StringBuffer sb : myList)
{
System.out.println(sb);
}
}


public static ArrayList <StringBuffer> getCombination (int n, int originalN, ArrayList <StringBuffer> myInputList)
{
ArrayList<StringBuffer> myOutput = new ArrayList <StringBuffer> ();
if (n == 0)
return myInputList;
if(myInputList == null || myInputList.size() == 0)
{
for(int i = 1; i <= originalN ; i ++)
{
StringBuffer sb = new StringBuffer();
sb.append(i);
myOutput.add(sb);
}
return getCombination(n-1, originalN, myOutput);
}
if(n > 0)
{
for(int i = 1; i <= originalN ; i ++)
{
for(StringBuffer inputSb: myInputList)
{
StringBuffer sb = new StringBuffer();
sb.append(i).append(inputSb);
myOutput.add(sb);
}
}
return getCombination(n-1,originalN, myOutput);
}

return null;

}
}

- Anonymous April 04, 2014 | 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