• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Sort an array according to the order defined by another array

August 19, 2014 by Dhaval Dave

Sort an array according to the order defined by another array
Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the elements will be same as those are in A2. For the elements not present in A2, append them at last in sorted order.
Input: A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
       A2[] = {2, 1, 8, 3}
Output: A1[] = {2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9}

The code should handle all cases like number of elements in A2[] may be more or less compared to A1[]. A2[] may have some elements which may not be there in A1[] and vice versa is also possible.
Given Code in: O(m*count + n) Space complexity O(m)
where m=size of A1,n=size of A2 count= maximum 
occurrence of any element of A1.

Company Asked : Amazon

#include <stdio.h>
#include <string.h>
int hash[1001]={0};
int main()
{
    //case b>a 
    //int b[]={1, 3, 2, 8, 7, 1, 9, 3, 6, 8, 8};
    //int a[]={2, 1, 1, 3};
    //Output : 1  1  3  2 
    
    //simple case a>b
    int a[]={2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8};
    int b[]={2, 1, 8, 3};
    //Output : 2  2  1  1  8  8  3  5  7  9  6 
    int al=sizeof(a)/sizeof(int);
    int bl=sizeof(b)/sizeof(int);
    int bmax=-1;
    int i,j;
    
    for(i=0;i<bl;i++){
        hash[b[i]]=1;
        bmax = bmax>=b[i]?bmax:b[i];
    }//O(m)
    for(i=0;i<al;i++){
       if(hash[a[i]]>=0) ++hash[a[i]];
    }//O(n)
    printf("nInput n");
    for(i=0;i<al;i++) {printf(" %d ",a[i]);}
    printf("nOrder n");
    for(i=0;i<bl;i++) {printf(" %d ",b[i]);}
    printf("nOutput n");
    
    for(i=0;i<bl;i++){
        if(hash[b[i]]>1) {
            --hash[b[i]];
            while(hash[b[i]]--) printf(" %d ",b[i]);
            }
    }//O(m*count)
    for(i=0;i<al;i++) {if(hash[a[i]]>0)printf(" %d ",a[i]);}
    //O(n)
    
return 0;
}

PS: Instead printing elements we can store it in auxiliary array and finally assign auxiliary array to Input array as required by AMAZON Interviewer.

You can visit Working code at http://ideone.com/4R0Zj4

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Given a sorted array and a number x, find the pair in array whose sum is closest to x

SAP Off Campus Hiring_ March 2015 Verbal Skills

Longest Increasing Subsequence

Binary Tree Isomorphic to each other

Printing each word reverse in string

Printing Longest Common Subsequence

Implement LRU Cache

Convert number to words java

Python Dictionaries

Serialise Deserialise N-ary Tree

Stock Buy Sell to Maximize Profit

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Given a float number convert it into the string WITHOUT using any inbuilt Function

Right view of Binary tree

Maximum sum contiguous subarray of an Array

TicTacToe Game As Asked in Flipkart

CodeChef Code SGARDEN

Binary Tree in Java

Python Array String

Word Break Problem

Memory Efficient LinkedList

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Trapping Rain Water

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

25 horses 5 tracks Find 3 fastest puzzle

SAP Interview Questions

Add Sub Multiply very large number stored as string

Practo Hiring Experience

LeetCode: Container With Most Water

Copyright © 2025 · Genesis Framework · WordPress · Log in