• 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

The Magic HackerEarth Nirvana solutions Hiring Challenge

Serialise Deserialise N-ary Tree

BFS (Breath First Search)

Maximum difference between two elements s.t larger element appears after the smaller number

Closed Parentheses checker

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

Connect n ropes with minimum cost

Cisco Hiring Event 21st – 22nd Feb 2015

Edit Distance ( Dynamic Programming )

Get K Max and Delete K Max in stream of incoming integers

Leetcode: Edit Distance

Find the smallest window in a string containing all characters of another string

SAP Hiring Off-Campus General Aptitude

Find two non repeating elements in an array of repeating elements

The greedy coins game Dynamic Programming

Best Java Book | Top Java Programming Book for Beginners

Code Chef PRGIFT Solution

Find loop in Singly linked list

Binary Tree in Java

Find min element in Sorted Rotated Array (Without Duplicates)

flattens 2 D linked list to a single sorted link list

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

Find Pythagorean Triplets in an array in O(N)

Maximum sum contiguous subarray of an Array

Sequence Finder Dynamic Programming

Wrong Directions given find minimum moves so that he can reach to the destination

25 horses 5 tracks Find 3 fastest puzzle

How strtok() Works

Circular Linked List

Print all nodes that are at distance k from a leaf node

Copyright © 2025 · Genesis Framework · WordPress · Log in