MAGMA Interview Question for Software Engineer / Developers






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

1. By using recursion , we can solve this problem.
2. You need two Arrays one is Permuted array P[] which stores permutation numbers.
second Array is Presence Arraya Presence[] which tells whether a particular number is already included in P[] or not .

3. Compiled and tested Code for this.

#include<stdio.h>

#define TRUE 1
#define FALSE 0

void PrintArray(int *P, int n)
{
	int i=0;
	while(i<n)
	{
		printf(" %d , ", P[i] );
		i++;
	}
	printf("\n");
}

/* 
int P[]: Permutation Array which stores numbers permutation 

int Presence[]:  it store either 0 or 1 for all numbers 1 - n . 
if Presence[i] == 1, i'th number already there in P[] array. 
So we will not include that number again.

int Count: tells so far how many numbers included in Permuted Array. 
*/

void Permutations(int *P, int *Presence, int n, int count )
{
	if( count >= n ||  n <= 0)
	{
		return;
	}
	else
	{
		int i = 1;

		while(i <= n)
		{
			if( Presence[i] != TRUE )
			{
				Presence[i] = TRUE;
				P[count] = i;
				if( count + 1 == n )
				{
					PrintArray(P,n); // Print all elements in P[]
				}
				else
				{
					Permutations(P, Presence, n, count + 1 );
				}
				Presence[i] = FALSE;
				
			}
			i++;
		}
	}
}

int main()
{
	int n = 5; // 1 to 10 numbers ermutations 
	int P[n] ; // Permutation Array, which store numbers permutation
	int Presence[n]; // it store either 0 or 1 for all numbers 1 - n 
	int i =0;

	/* Intializing arrays */
	while( i < n )
	{
		P[i] = Presence[i] = 0;
		i++;
	}
 
	Permutations(P, Presence, n, 0 );

	return 0;
}

Is there any other better way ?

- siva.sai.2020 February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

using one array is sufficient

- siva.sai.2020 March 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

U can do it with single array...Call Perm(a,0).a has the integers 1 to n

Perm(int[] a,int start){
  if(start==a.length){
    Print a
    return;
  }
  int temp;
  for(int i=start;i<a.length;i++){
    swap(a[start],a[i])
    Perm(a,start+1)
    swap(a[start],a[i])
  }
}

- Sathya February 17, 2011 | 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