I recently performed online assessment for role of JP Morgan and Chase..... the HR told me it would be two easy problems and then I would have to explain the solution webcam/audio is required for this.
Instead I've got a medium and a hard problem.
Q1: Find the sum of distance between consecutive numbers, numbers are non-repetitive and can be in any random order.
I solved this by first iterating over the list and stored values as keys and indexes as value.
Then Iterate again over the list and if I find values+1 or value -1 in the map, I calculated the difference of indexes and add it into TotalSum variable. After adding we should not forget removing the current value from map as numbers are non-repetitive so we must not be checking it again.
Q2: Sort the list of Integers with minimum number of swaps (Its a popular hackrrank interview kit question), plus also sort it in a way that even numbers are at the start of list in ascending order and the odd numbers are at the end of list in descending order.
Ans: I first iterated over list using two-pointers and swapped positions if first even numbers' index from left is greater than first odd numbers' index from right. (also whenever swap do swaps++)
And After that I applied the minimum swap algo for (0 to l) indexes and add to the swaps count. Same algo s applied in reverse from (r, n) and added into swaps count.
I'm definately not sure about the exact solution at this moment for 2nd question as few test cases failed.