TECHNICAL INTERVIEW QUESTION on DATA STRUCTURES & ALGORITHMS
This presentation is the property of its rightful owner.
Sponsored Links
1 / 9

Count Inversion PowerPoint PPT Presentation


Was 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.

Download Presentation

Count Inversion

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Count inversion

TECHNICAL 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.


What is an inversion

What is an inversion?

Let A be an array of n distinct numbers. If i < j and A[i] > A[j], 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 = i+1 to N

if(A[i] > A[j])

countInversions++;

Statement S1 will be executed (N-1) + (N-2) + … + 1 times = (N-1) * N / 2

Overall time complexity is O(n2)

S1


Can you write an algorithm to count the number of inversions with time complexity o n log n

Can you write an algorithm to count the number of inversions with time complexity O(n log n)?

Hint: Merge Sort


Let s look at merge sort

Let’s look at Merge-Sort

mid

start

end

MERGE-SORT(A, start, end) {

if(start < end) {

mid = (start + end) / 2;

MERGE-SORT(A, start, mid);

MERGE-SORT(A, mid + 1, end);

MERGE(A, start, mid, end);

}

}

end

mid+1

mid

start


Merge operation

Merge 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 A[start: mid] to firstArray[0: mid-start];

Copy values from A[mid+1: end] to secondArray[0: end-mid-1];

firstArray[mid-start+1] = ∞

secondArray[end-mid] = ∞

i = 0, j = 0;

for k = start to end {

if(firstArray[i] <= secondArray[j]) {

A[k] = firstArray[i];

i++;

} else {

A[k] = secondArray[j];

j++;

}

}

}

mid

start

end

A

end

mid+1

mid

start

j

i

secondArray

firstArray

A

k


Can you modify mergesort to count the number of inversions

Can you modify MergeSort to count the number of inversions?


Modified merge sort

Modified Merge-Sort

mid

start

end

int MERGE-SORT(A, start, end) {

intcountInversions = 0;

if(start < 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

mid+1

mid

start


Modified merge operation

Modified Merge Operation

intMERGE(A, start, mid, end) {

firstArray = new Array of size mid – start + 2;

secondArray = new Array of size end – mid + 1;

Copy values from A[start: mid] to firstArray[0: mid-start];

Copy values from A[mid+1: end] to secondArray[0: end-mid-1];

firstArray[mid-start+1] = ∞

secondArray[end-mid] = ∞

i = 0, j = 0, countInversions = 0;

for k = start to end {

if(firstArray[i] <= secondArray[j]) {

A[k] = firstArray[i];

i++;

} else {

A[k] = secondArray[j];

j++;

countInversions += firstArray.length – i - 1;

}

}

return countInversions;

}

mid

start

end

A

end

mid+1

mid

start

j

i

secondArray

firstArray

A

k


Was this content useful

Was this Content Useful?

Comment

Share

Like

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.


  • Login