What I remember from my Data Science skills interviews - Part 3

This is a continuation from my previous post (What I remember from my Data Science skills interviews - Part 2). 

Note: The example below is not an exact replica of any test I have done.


#A PROGRAM THAT RETURNS THE SMALLEST SUBSET OF NUMBERS IN A SET(FROM SMALLEST TO LARGEST) WHOSE SUM IS GREATER THAN THAT OF THE REMAINING SUBSET

(All the code is in R)

values_check = function(k){

  values<-max(k)

  for (i in 2:length(k)) {

    b<-sort(k, decreasing = TRUE)

    if(sum(b[1:i-1])<sum(b[i:length(b)])){

      values<-append(values,b[i])

    }

    if(sum(b[1:i-1])>sum(b[i:length(b)])){

      break

    }

  }

  sort(values)

}


Examples

values_check(c(4,3,5,8,4,6))

Results: 5,6,8

a <- array(c(7,4,6,9,2))

values_check(a)

Results: 7,9


Breakdown

Before the check begins, arrange them from largest to smallest

It is a given that the largest number will be in the answer subset.

If the sum of the first number vs the rest is smaller, 

Add the next largest number to the subset containing the largest number

Stop at the point where the sum of the large numbers is greater than the rest.


Happy exploring!!

~NMN

Comments

Popular posts from this blog

Financial Mathematics CT-1 Finally Paid Off

Data Scientist Courses (edX vs DatCamp)

Self Joins in R