Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Just do it the way we manually do it :
Sort the string and swap elements of the string in lexicographic order, while doing this keep count of the number strings that come before the current string in lexicographic order.
Also take care of duplicates while counting and swapping.
Eg:
For string 'dbace' :
1. sort : abcde
2. count permutation starting with a : 24
3. count permutation starting with b : 24
4. count permutation starting with c : 24
5. count permutation starting with 2nd letter as a : 6

int findCount(string str, int start, int end)
{
   int length = end-start+1;
   int count = factorial(length);
   int i=start;
   while(i< end)
   {
     int rep=1; 
     bool flag = true; 
     while(str[i] == str[i+1]) //counting number of repitions
     {
        flag = false;
        rep++;        
        i++;
     }
     if(flag)
      i++;
     count = count/ factorial(rep); //dividing by repitions!
   }
   return count;
}
int permRank(string str)
{
     if(str == "")
      return 0;
     int count =0;
     string sorted(str);
     sort(sorted.begin(),sorted.end());
     
     for(int i=0;i<sorted.length()-1;i++)
     {
        int rem;
        int j=i+1;
        
        while(sorted[i] != str[i])
        {
          rem = findCount(sorted,i+1,sorted.length()-1);
          count += rem;
          cout<<sorted<<"--->"<<rem<<endl;
          while(sorted[i] == sorted[j]) // don't swap if there is a duplicate
             j++; 
          swap(sorted,i,j);
          j++;          
        }
     }
     return count;
}

- CodePredator December 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not work if char are repeating ....
Eg : aabcd

- MVVSK December 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have taken care of repetitions.
I have put comments for those lines. Is there a problem in that line?

- CodePredator December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One small error: return count+1 instead of count in permRank(string str) function, Except it this code is correct for all cases including repeating char case,

- Deepak Singhal January 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

what if charcters are not necessarily unique?

- Gupt December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

K=n-1//n is the string length
For(i=0 to n)
{
      For(j=i+1 to n )
      {
           If(a[i]>a[j])
               S+= k!
       }
}return s+1

- Mr.x December 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be broken into sub-problems. Here is the recurrence relation

LexicographicRank(A) =  1  (when A has just 1 char or if A is empty)
                  otherwise
LexicographicRank(A) =  Rank(A.charAt(0), A) * LexicographicRank(A.substring(1))
Rank(char c, String A)  = #(unique characters in A < c) + 1
e.g rank of C in "CDA" is 2

- Looper December 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the exact recurrence relation wd be
LexicographicRank(A) = 1 (when A has just 1 char or if A is empty)
otherwise
LexicographicRank(A) = LexicographicRank(A.substring(1)) + rank(A(0), A.Substring(1)) * factorial(lengthofA - 1)
where Rank(char c, String A) = #(unique characters in A < c)

- Gupt December 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Sort the permuation using LSD or MSD sort with O(n)
2) put the rank and permutation into hash table
3)Search

- Rocky December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ranking(String s1){
	String sorted =sort(s1);
        int rank =0,temp=0,len =sorted.length();
        for(int i=0;i<len;i++){
            int start =0;
            int flag =0;
            int remLen =sorted.length()-start-1;
            while(start<sorted.length()){
                if(s1.charAt(i)!=sorted.charAt(start)){
                    rank = rank + factorial(remLen);
                }
                if(flag==0){
                    String c =sorted.charAt(start)+"";
                    sorted =sorted.replaceFirst(c,"");
                    len =sorted.length();flag=1;
                    break;
                }
                start++;
            }
        }
        return (rank+1);
}

- cravi24 December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

int cnt[30];
lli ncr[20][20];
// this function will return number of valid string of n remaining characters
lli count(int n)
{
lli ret = 1;

for (int i = 0; i < 26; i++)
{
ret *= ncr[n][cnt[i]];
n -= cnt[i];

}
return ret;
}

int main()
{
for(int i=0;i<=20;i++)
{
ncr[i][0]=1;
ncr[i][i]=1;
for(int j=1;j<i;j++)
{
ncr[i][j]=ncr[i-1][j]+ncr[i-1][j-1];
}
}



int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int len=s.length();
for(int i=0;i<len;i++)
{
cnt[s[i]-'a']++;
}

lli ans=0;
for(int i=1;i<=len;i++)
{

// cout<<i<<endl;
for(int j=0;j<=25;j++)
{
if(s[i-1]>(j+'a') && cnt[j])
{

cnt[j]--;
ans+=count(len-i);
cnt[j]++;
}
}

cnt[s[i-1]-'a']--;


}
cout<<ans+1<<endl;
}
}

- Anonymous May 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

int cnt[30];
lli ncr[20][20];
// this function will return number of valid string of n remaining characters
lli count(int n)
{
lli ret = 1;

for (int i = 0; i < 26; i++)
{
ret *= ncr[n][cnt[i]];
n -= cnt[i];

}
return ret;
}

int main()
{
for(int i=0;i<=20;i++)
{
ncr[i][0]=1;
ncr[i][i]=1;
for(int j=1;j<i;j++)
{
ncr[i][j]=ncr[i-1][j]+ncr[i-1][j-1];
}
}



int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int len=s.length();
for(int i=0;i<len;i++)
{
cnt[s[i]-'a']++;
}

lli ans=0;
for(int i=1;i<=len;i++)
{

// cout<<i<<endl;
for(int j=0;j<=25;j++)
{
if(s[i-1]>(j+'a') && cnt[j])
{

cnt[j]--;
ans+=count(len-i);
cnt[j]++;
}
}

cnt[s[i-1]-'a']--;


}
cout<<ans+1<<endl;
}
}
}}

- deepak gautam May 03, 2016 | 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