• 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

Count number of ways to reach a given score in a game

Common Ancestor in a Binary Tree or Binary Search Tree

Find the kth number with prime factors 3, 5 and 7

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

VMWare Openings

Find min element in Sorted Rotated Array (Without Duplicates)

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

Reversal of LinkedList

Print Power Set of a Set

Memory Efficient LinkedList

K’th Largest Element in BST when modification to BST is not allowed

SAP Hiring Off-Campus General Aptitude

Length of the longest substring without repeating characters

Find if a binary tree is height balanced ?

Search element in a matrix with all rows and columns in sorted order

DFS (Depth First Search)

SAP Interview Questions

Skiing on Mountains Matrix

CodeChef’ RRCOPY

Templates in C++

The Magic HackerEarth Nirvana solutions Hiring Challenge

Sequence Finder Dynamic Programming

Circular Linked List

Find Percentage of Words matching in Two Strings

Calculate price of parking from parking start end time prices

Find two non repeating elements in an array of repeating elements

simple sql injection

Find position of the only set bit

Apriori algorithm C Code Data Mining

Urban Ladder Written Test.

Copyright © 2025 · Genesis Framework · WordPress · Log in