## Cadence Inc Interview Question for SDE1s

• 0
of 2 votes

Country: India
Interview Type: In-Person

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

This is permutation problem where we need to form the number with all the digit given in the array. Below is the working code for the same.
Call the function with "permute(arr, 0, sizeOfArray);"

``````void permute(int *a, int i, int n)
{
int j;
if (i == n)
printf("%d\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));	//Here swap function will swap the value of its argument.
permute(a, i+1, n);
swap((a+i), (a+j)); 	//Here swap function will swap the value of its argument.
}
}
}``````

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

given a array of digits. print all combination of of these i.e all no formed by these.
repetition allowed.

and then for modification:
repetition not allowed

example:
i/p:
arr={2,3,4}

o/p:
(without repetition)
234
243
324
342
423
432

========

Your example, if repetition was allowed, what would the o/p be?
Type your questions clearly. These are permutations not combinations.

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

``````def permute(xs):
"""Generate all permutations of `xs`

Example:
>>> list(permute([0, 1, 2]))
[[0, 1, 2], [1, 0, 2], [1, 2, 0], [0, 2, 1], [2, 0, 1], [2, 1, 0]]
"""
if len(xs) <= 1:
yield xs
return

h, ts = xs[0], xs[1:]
for t in permute(ts):
for i in range(len(ts) + 1):
yield t[:i] + [h] + t[i:]``````

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

void permute(int *a, int i, int n)
{
int j,k;
if (i == n)
{
for(k=0;k<=n;k++)
printf("%d", a[k]);
printf("\n");
}
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j)); //Here swap function will swap the value of its
permute(a, i+1, n);
swap((a+i), (a+j)); //Here swap function will swap the value of its
}
}
}

main()
{
int a[] = {1,2,3};
permute(a,0,2);
}

int swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

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

If you use C++, the best solution is to use std::next_permutation:

``````#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
int n = 0;
std::cin >> n;

std::vector<int> nums(n);
for(int i = 0; i < n; i++)
std::cin >> nums[i];

std::sort(nums.begin(), nums.end());
do
{
for(int i = 0; i < n; i++)
std::cout << nums[i];
std::cout << std::endl;
}
while(std::next_permutation(nums.begin(), nums.end()));

return 0;
}``````

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

#include <iostream>

using namespace std;

bool isValid0(int out[], int size) {
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size; ++j) {
if (out[i] == out[j]) return false;
}
}
return true;
}
void auxPerm0(int d, int in[], int out[], int size) {
if (d == size) {
if (isValid0(out, size))
{
for (int i = 0; i < size; ++i)
cout << out[i] << " ";
cout << endl;
}
return;
}
for (int i = 0; i < size; ++i) {
out[d] = in[i];
auxPerm0(d + 1, in, out, size);
}
}

void permutations0(int in[],int size) {
int *out = new int[size];
auxPerm0(0, in, out, size);
}

int main()
{
int ar[] = { 2,3,4 };
int n = sizeof(ar) / sizeof(ar[0]);
permutations0(ar,n);
return 0;
}

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