Programming Test MKI

Question Type: logic

Questions

  1. Find the shortest path from cities A and N and their associated trajectories from the graph below . The weight on the vertices represents the distance between two cities in meters!

    001

  2. A left-truncatable prime is a prime number that contains no 0 digits and, when the first digit is successively removed, the result is always prime. A right- truncatable prime is a prime number that contains no 0 digits and, when the last digit is successively removed, the result is always prime. Create a function that takes an integer as an argument and:

    • If the integer is only a left-truncatable prime, return “left”.
    • If the integer is only a right-truncatable prime, return “right”.
    • If the integer is both, return “both”.
    • Otherwise, return False.
  3. Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W and return all values in val that meet these conditions. You cannot break an item, either pick the complete item or don’t pick it (0-1 property)!

  4. A group of n prisoners stand in a circle awaiting execution. Starting from an arbitrary position(0), the executioner kills every kth person until one person remains standing, who is then granted freedom (see examples). Create a function that takes 2 arguments — the number of people to be executed n, and the step size k, and returns the original position (index) of the person who survives.

    who_goes_free(9, 2) ➞ 2
    
    # Prisoners = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    # Executed people replaced by - (a dash) for illustration purposes.
    # 1st round of execution = [0,-,2,-,4,-,6,-,8] -> [0, 2, 4, 6, 8]
    # 2nd round = [-,2,-,6,-] -> [2,6] # 0 is killed in this round because it's beside 8 who was skipped over.
    # 3rd round = [2, -]
    
    who_goes_free(9, 3) ➞ 0
    
    # [0, 1, 2, 3, 4, 5, 6, 7, 8]
    # [0, 1, -, 3, 4, -, 6, 7, -] -> [0, 1, 3, 4, 6, 7]
    # [0, 1, -, 4, 6, -] -> [0, 1, 4, 6]
    # [0, 1, -, 6] -> [0, 1, 6]
    # [0, -, 6] -> [0, 6]
    # [0, -] -> [0]