Goldman Sachs | OA | Hackerrank
9829

Hackerrank OA 2 questions 120 min

  1. Tag Identification Number
    You're given a string containing a list of digits. You must make as many IDs of the format 8xxxxxxxxxx (an 8 followed by 10 digits) as possible. Return the number of IDs you can make. The IDs do not have to be unique, and you may rearrange the digits, but you may only use each digit once.
def numOfIds(pool):
	s = len(pool)/11
	c = 0
	for i in range(0,len(pool)):
		if pool[i] == '8':
			c+=1
	return min(c, int(s))
  1. Inversion is a strictly decreasing subsequence of length 3. More formally, given an array, p, an inversion in the array is any time some p[i] > p[j] > p[k] and i < j < k. Given an array of length n, find the number of inversions.
    Example:
    n = 5, arr = [5, 3, 4, 2, 1]
    Array inversions are [5, 3, 2], [5,3,1], [5,4,2], [5,4,1], [5,2,1], [3,2,1], [4,2,1]
    n = 4, arr = [4,2,2,1]
    The only inversion is [4,2,1] and we do not count the duplicate inversion.

Solution in O(n)^2
NOTE: Only 10/12 test cases passed

def getInvCount(arr):
   c = 0   
   for i in range(1,n-1):
       r = 0
       for j in range(i+1 ,n):
           if (arr[i] > arr[j]):
               r+=1
       l = 0;
       for j in range(i-1,-1,-1): #Tricky remember
           if (arr[i] < arr[j]):
               l+=1
       c += int(l*r)
    
   return c
Comments (8)