Basics

This page deals with Basic Python Questions

There are some common programming techniques that you must be familiar with if you want to be comfortable enough to solve Python questions live during an interview. Here we have summarized a few of such techniques but do hone your skills using platforms like Leet code to ensure that you perform well during interviews.

We have collated some resources from the internet as a starting point to help you prepare on this:

Questions

[SNAPCHAT] Palindrome Checker

Given a string, determine whether any permutation of it is a palindrome.

For example, carerac should return true, since it can be rearranged to form racecar, which is a palindrome. sunset should return false, since there’s no rearrangement that can form a palindrome.

Answer

Pattern Generation

Write a function to generate this pattern: 1 2 3 4 5 6

Now change the code to output 1 1 2 1 2 3

Answer

[UBER] Sum to N

Given a list of positive integers, find all combinations that equal the value N.

Example:

integers = [2,3,5], target = 8,

output = [[2,2,2,2],[2,3,3],[3,5]]

Answer

We will solve it in 2 ways, one using itertools the other one using recursion:

[STARBUCKS] Max Profit

Given a list of stock prices in ascending order by datetime, write a function that outputs the max profit by buying and selling at a specific interval.

Example:

stock_prices = [10,5,20,32,25,12]

buy –> 5 sell –> 32

Answer

[IBM] Isomorphic string check

Write a function which will check if each character of string1 can be mapped to a unique character of string2.

Example: string1 = ‘donut’ string2 = ‘fatty’

string_map(string1, string2) == False # as n and u both get mapped to t

string1 = ‘enemy’ string2 = ‘enemy’

string_map(string1, string2) == True # as e’s get mapped to e even though there is two e

string1 = ‘enemy’ string2 = ‘yneme’

string_map(string1, string2) == False # as e’s dont get mapped uniquely

Answer

[WORKDAY] Sorted String merge

Given two sorted lists, write a function to merge them into one sorted list.

What’s the time complexity?

Answer

[POSTMATES] Weekly Aggregation

Given a list of timestamps in sequential order, return a list of lists grouped by week (7 days) using the first timestamp as the starting point.

Example:

ts = [ ‘2019-01-01’, ‘2019-01-02’, ‘2019-01-08’, ‘2019-02-01’, ‘2019-02-02’, ‘2019-02-05’, ]

output = [ [‘2019-01-01’, ‘2019-01-02’], [‘2019-01-08’], [‘2019-02-01’, ‘2019-02-02’], [‘2019-02-05’] ]

Answer

[MICROSOFT] Find the missing number

You have an array of integers of length n spanning 0 to n with one missing. Write a function that returns the missing number in the array

Example:

nums = [0,1,2,4,5] missingNumber(nums) -> 3

Complexity of O(N) required.

Answer

[SQUARE] Book Combinations

You have store credit of N dollars. However, you don’t want to walk a long distance with heavy books, but you want to spend all of your store credit.

Let’s say we have a list of books in the format of tuples where the first value is the price and the second value is the weight of the book -> (price,weight).

Write a function optimal_books to retrieve the combination allows you to spend all of your store credit while getting at least two books at the lowest weight.

Note: you should spend all your credit and getting at least 2 books, If no such condition satisfied just return empty list.

Example:

Answer

[WISH] Intersecting Lines

Say you are given a list of tuples where the first element is the slope of a line and the second element is the y-intercept of a line.

Write a function find_intersecting to find which lines, if any, intersect with any of the others in the given x_range.

Example

tuple_list = [(2, 3), (-3, 5), (4, 6), (5, 7)] x_range = (0, 1)

Output

def find_intersecting(tuple_list, x_range) -> [(2,3), (-3,5)]

Answer

Find the majority element in a list.

a = [2,3,4,6, 6, 2,2] answer --> 2

Answer

[INTUIT] Iterator

Implement an iterator function which takes three iterators as the input and sorts them.

Answer

2 Solutions are provided below:

[SPLUNK] Last Page Number

We're given a string of integers that represent page numbers.

Write a function to return the last page number in the string. If the string of integers is not in correct page order, return the last number in order.

Answer

Python Recursion

Explain Python recursion with an example.

Answer

Recursion is a programming technique that allows a function to call itself. This can be useful for solving problems that involve self-similar structures, such as trees and graphs.

This function works by calling itself recursively to calculate the factorial. The base cases are when n is 0 or 1, in which case the function simply returns n.

Recursion can be a powerful tool, but it is important to use it carefully. If a recursive function is not designed carefully, it can easily lead to stack overflows.

[INTUIT] Subarray Product Less Than K

This was asked in INTUIT Sr. Data Scientist initial round using Glider

Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

Example 1:

Example 2:

Constraints:

  • 1 <= nums.length <= 3 * 104

  • 1 <= nums[i] <= 1000

  • 0 <= k <= 106

Answer

You are not only required to solve the problem in a limited time frame (~30mins) but the ask is also to ensure that the all test-cases pass and at least one of them fails if the code does not meet the required time complexity even if you get the required answer.

The above code fails some test cases as it has O(n^3) complexity. There is the two-pointer or inchworm approach given below which solves the problem along with taking care of the complexity. Click on the Leetcode link above and check the discussions if you want to understand it better:

Reverse Vowels in a String

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1:

Example 2:

Answer

This can be solved using the 2-pointer approach:

Swap numbers

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Example:

Answer

Algorithm complexity: Time complexity: O(n). Our fast pointer does not visit the same spot twice. Space complexity: O(1). All operations are made in-place

[SALESFORCE] Interpolation

Answer

[SALESFORCE] Prison Problem

Answer

Last updated

Was this helpful?