No results found for the specified position. 12 Python Beginner Level Arrays Interview Questions

MockQuestions

Python Beginner Level Arrays Mock Interview

To help you prepare for your Python Beginner Level Arrays interview, here are 12 interview questions and answer examples.

Get More Information About Our Python Beginner Level Arrays Interview Questions

Question 1 of 12

Find integers smaller than the current number.

This question proves the developer's ability to work with elements.

You are given an array of integers num. Your task is to find the count of integers that are less than num[i] for every integer num[i] in the array num. You need to return these counts in an array count, with count[i] representing the count of integers less than num[i].

/*Example*/

num = [5, 1, 2, 2, 4, 3, 10, 3]
expected result- [6, 0, 1, 1, 5, 3, 7, 3]
for num[0] = 5, there are 6 integers smaller than 5 (1, 2, 2, 4, 3, 3)
for num[1] = 1, there are 0 integers smaller than 1
for num[2] = 2, there is 1 integer smaller than 2 (1)
for num[2] = 2, there is 1 integer smaller than 2 (1)
for num[3] = 4, there are 5 integers smaller than 4 (1, 2, 2, 3, 3)
for num[4] = 3, there are 3 integers smaller than 3 (1, 2, 2)
for num[5] = 10, there are 7 integers smaller than 10 (5, 1, 2, 2, 4, 3, 3)
for num[6] = 3, there are 3 integers smaller than 3 (1, 2, 2)

Solution:

First, we need to count the number of integers that are less than integer num[i]. To find this, we can make a copy of the given and sort it. Then, the count of integers less than sorted[i] will be equal to I:

Given array- num = [5, 1, 2, 2, 4, 3, 10, 3]
Sorted array- sorted = [1, 2, 2, 3, 3, 4, 5, 10]
For 1, index = 0, so count of integers less than 1 = 0
For 2, index = 1 (we will see smallest index), so count of integers less than 2 = 1
For 3, index = 3 (we will see smallest index), so count of integers less than 3 = 3
For 4, index = 5, so count of integers less than 4 = 5
For 5, index = 6, so count of integers less than 5 = 6
For 10, index = 7, so count of integers less than 10 = 7

We will store these counts in a map, where the keys will be the integer itself and the values will be the count of integers less than that integer. So resulting map will be:

{
1 -> 0
2 -> 1
3 -> 3
4 -> 5
5 -> 6
10 -> 7
}

Then, we will create a result array count and will store counts corresponding to each element num[i].

def findSmallerIntegers(nums):
    sorted_nums = sorted(nums)
    lesser_than_count = {}

    for i, num in enumerate(sorted_nums):
        # if ele is not already present than update its
        # entry in map with value equal to index i of 
        # num in sorted array
        if num  not in lesser_than_count:
            lesser_than_count[num] = i

    # for each element in nums take it's lesser than count
    return [lesser_than_count[ele] for ele in nums]

Time complexity- O(n)
Space complexity- O(n)

Written by on June 27th, 2021

Next Question

12 Python Beginner Level Arrays Interview Questions & Answers

Below is a list of our Python Beginner Level Arrays interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Find integers smaller than the current number.

  • 2. Create a solution to return a rearranged array.

  • 3. How do you find the sum of unique elements?

  • 4. Can you find the richest person and return their wealth?

  • 5. Create a target array in a given order.

  • 6. How do you sort by parity?

  • 7. What ways can you reach the top of the steps?

  • 8. Create a code to return the maximized sum.

  • 9. Find which kids can be happy after extra candy.

  • 10. Can you find the highest altitude?

  • 11. What is the diagonal sum?

  • 12. Find squares of a sorted array.