Title: Count Inversions
1TECHNICAL INTERVIEW QUESTION on DATA STRUCTURES
ALGORITHMSCount the number of inversions in an
array
CodeGround Online Testing Platform is an online
assessment and evaluation system that helps
Recruiters conduct online screening tests to
filter candidates before the interview process.
CodeGround Recruitment Tests can be used during
Campus Recruitment or screening walk-in
candidates. CodeGround supports Aptitude Tests,
English Communication Skills Assessments and
Online Coding Contests in C, C, Java, PHP,
Ruby, Python and JavaScript. CodeGround also
supports asynchronous automated interviews.
2What is an inversion?
- Let A be an array of n distinct numbers. If i lt j
and Ai gt Aj, then the pair (i, j) is called
an inversion of A. - For example, the array 2,3,8,6,1 has 5
inversions (2,1) (3,1) (8,6) (8,1) and (6,1)
Trivial solution
countInversions 0 for i 1 to N for j
i1 to N if(Ai gt Aj)
countInversions Statement S1 will be executed
(N-1) (N-2) 1 times (N-1) N /
2 Overall time complexity is O(n2)
S1
3Can you write an algorithm to count the number of
inversions with time complexity O(n log n)?
Hint Merge Sort
4Lets look at Merge-Sort
mid
start
end
MERGE-SORT(A, start, end) if(start lt end)
mid (start end) / 2
MERGE-SORT(A, start, mid) MERGE-SORT(A,
mid 1, end) MERGE(A, start, mid, end)
end
mid1
mid
start
5Merge Operation
MERGE(A, start, mid, end) firstArray
new Array of size mid start 2
secondArray new Array of size end mid 1
Copy values from Astart mid to firstArray0
mid-start Copy values from Amid1 end to
secondArray0 end-mid-1 firstArraymid-star
t1 8 secondArrayend-mid 8 i 0,
j 0 for k start to end
if(firstArrayi lt secondArrayj)
Ak firstArrayi i
else Ak secondArrayj
j
mid
start
end
A
end
mid1
mid
start
8
j
i
8
secondArray
firstArray
A
k
6Can you modify MergeSort to count the number of
inversions?
7Modified Merge-Sort
mid
start
end
int MERGE-SORT(A, start, end) int
countInversions 0 if(start lt end)
mid (start end) / 2 countInversions
MERGE-SORT(A, start, mid)
countInversions MERGE-SORT(A, mid 1, end)
countInversions MERGE(A, start, mid,
end) return countInversions
end
mid1
mid
start
8Modified Merge Operation
int MERGE(A, start, mid, end) firstArray
new Array of size mid start 2
secondArray new Array of size end mid 1
Copy values from Astart mid to firstArray0
mid-start Copy values from Amid1 end to
secondArray0 end-mid-1 firstArraymid-star
t1 8 secondArrayend-mid 8 i 0,
j 0, countInversions 0 for k start to
end if(firstArrayi lt
secondArrayj) Ak
firstArrayi i else
Ak secondArrayj
j countInversions
firstArray.length i - 1
return countInversions
mid
start
end
A
end
mid1
mid
start
8
j
i
8
secondArray
firstArray
A
k
9Was this Content Useful?
Like
Share
Comment
CodeGround Online Testing Platform is an online
assessment and evaluation system that helps
Recruiters conduct online screening tests to
filter candidates before the interview process.
CodeGround Recruitment Tests can be used during
Campus Recruitment or screening walk-in
candidates. CodeGround supports Aptitude Tests,
English Communication Skills Assessments and
Online Coding Contests in C, C, Java, PHP,
Ruby, Python and JavaScript. CodeGround also
supports asynchronous automated interviews.