Hard Level practice questions in Python

WAP stands for Write A Program

  1. Given a list of names. Create a list of number of vowels in each name from the given list.
    Given: l = [‘Nimisha’, ‘Harshil’, ‘Naman’, ‘Saurabh’, ‘Manish’]
    Output: [3, 2, 2, 3,2]
  2. Given a list of repeating characters. Output a compressed string showing the frequency of all the characters from the list.
    Input: chars = [“a”,”a”,”b”,”b”,”c”,”c”,”c”]
    Output: “a2b2c3”
  3. There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has. Create a list showing by what number(quantity) the candies needs to be redistributed so that all the kids have equal number of candies.
    Input: candies = [2,3,6,1,3],
    Output: [1, 0, -3, 2, 0]
  4. Given an input string s, reverse the order of the words.
    Input: s = ‘How are you’
    Output: so = ‘You are How’
  5. Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

    Example 1:
    Input: nums = [1,2,3,4]
    Output: [24,12,8,6]
    Example 2:
    Input: nums = [-1,1,0,-3,3]
    Output: [0,0,9,0,0]

  6. Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

    Example 1:
    Input: s = “abc”, t = “ahbgdc”
    Output: True
    Example 2:
    Input: s = “axc”, t = “ahbgdc”
    Output: False

  7. A peak element is an element that is strictly greater than its neighbors.
    Given an integer list nums, find a peak element, and return it. If the list contains multiple peaks, return all of them.
    Example 1:
    Input: nums = [1,2,3,1]
    Output: 3
    Example 2:
    Input: nums = [1,2,1,3,5,6,4]
    Output: 2, 6

  8. You are playing an online game. In the game, a list of N numbers is given. The player has to arrange the numbers so that all the odd numbers of the list come after the even numbers. Write an algorithm to arrange the given list such that all the odd numbers of the list come after the even numbers.
    Input: [10, 98, 3, 33, 12, 22, 21, 11]

    Output: [10, 98, 12, 22, 3, 33, 21, 11]

  9. Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
    For example, there are n=7 socks with colors ar = {1,2,1,2,1,3,2}. There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.
    Input: l = [10, 20, 20, 10, 10, 30, 50, 10, 20]
    Output: 3
    Logic: Alex can match 3 pairs of socks i.e 10-10, 10-10, 20-20
    while the left out socks are 50, 60, 20