• 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

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

Print vertical sum of all the axis in the given binary tree

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Find if a binary tree is height balanced ?

How strtok() Works

Find and print longest consecutive number sequence in a given sequence in O(n)

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Hackerearth : Counting Subarrays

CodeChef’ RRCOPY

Maximum of all subarrays of size k

Find Nearest Minimum number in left side in O(n)

Maximum size of square sub matrix with all 1’s in a binary matrix

Templates in C++

Sort Stack in place

Python Dictionaries

Convert number to words java

Find two non repeating elements in an array of repeating elements

DFS (Depth First Search)

Test Cases for Round Function

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

BlueStone E-commerce Interview Experience

Spanning Tree

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Code Chef PRGIFT Solution

Leetcode: Edit Distance

System Design: Designing a LLD for Hotel Booking

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

Find min element in Sorted Rotated Array (With Duplicates)

Check Binary Tree is Binary Search Tree or not

Copyright © 2025 · Genesis Framework · WordPress · Log in