SWE interview questions

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

list comprehension

Python rules for creating lists intelligently s = [x for x in range(1:51) if x%2 == 0]

find all the permutations of a given array of distinct integers

- initialize an empty list to store all permutations - defin recursive helper function backtrack to generate permutations - base case: if the current permutation is of length n - recursive: for each unused integer in the input array, add it to the current permutation and recurse the current permutation using backtracking, and pop

Given an integer array arr, return the length of a maximum size turbulent subarray of arr. A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if: For i <= k < j:arr[k] > arr[k + 1] when k is odd, andarr[k] < arr[k + 1] when k is even.

- initialize variables - handle edge case of array of length 1 - loop through the array - move the right pointer to next valid element by using this comparison - update output with the maximum subarray length - move pointers to the next subarray - return output

You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree. In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent. Return the minimum number of moves required to make every node have exactly one coin.

1. recursively go through right and left subtrees 2. dfs, assign right dfs recursion and left dfs recursion 3. update the result to be +=abs(left)+abs(right) 4. return the root value and left and right subtree values -1

map function

A function that generates an output list from an input list by applying a function to each element in the input list. eg. map(function, list)

GET request

An HTTP method in which the client requests data such as a webpage. The client might provide some data as part of the request in the query string of the URL. GET requests can be cached GET requests remain in the browser history GET requests can be bookmarked GET requests should never be used when dealing with sensitive data GET requests have length restrictions GET requests are only used to request data (not modify) GET /genapp/customers?name=Joe%20Bloggs/ 1 Host: www.example.com

DP alg for fibonacci

Initialize an array of size n+1 to store the Fibonacci sequence values. Set the first two elements of the array to 0 and 1, respectively. Loop through the array from index 2 to n, and for each index i, set the value of the i-th element to the sum of the (i-1)-th and (i-2)-th elements. Return the n-th element of the array as the result.

DP for longest common sequence

Initialize an m x n matrix to store the LCS values. Set the first row and first column of the matrix to 0. Loop through the matrix from row 1 and column 1. If the characters in both strings match, set the current cell to the value of the diagonal cell plus 1. If the characters in both strings do not match, set the current cell to the maximum value of the left and top cells. The value of the bottom-right cell in the matrix will be the length of the LCS. To retrieve the LCS, traverse the matrix from the bottom-right cell to the top-left cell, following the cells with the maximum value and adding the corresponding characters to the result.

Quick Sort

Non-stable, in place sort with an order of growth of NlogN. Needs lgN of extra space. It has a probabilistic guarantee. Works by making use of a divide and conquer method. The array is divided into two parts, and then the parts are sorted independently. An arbitrary value is chosen as the partition. Afterwards, all items which are larger than this value go to the right of it, and all items which are less than this value go to the left of it. We arbitrarily choose a[lo] as a partitioning item. Then we scan from the left end of the array one by one until we find an entry that is greater than a[lo]. At the same time, we are scanning from a[lo] to the right to find an entry that is less than or equal to a[lo]. Once we find these two values, we swap them.

sorted function

The Python function that accepts a list as an argument, and will return a new, sorted list containing the same elements as the original. sorted(animals, key=lambda animal: animal['age'])

POST request

This HTTP request method is preferred when the request contains data that will be stored on the server, and unintentionally repeating the request could cause an error such as repeating a charge on a credit card

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

binary search: while loop, check if low<=high middle=(low+high)//2 if mid is less than target, low=mid+1 else high=mid-1

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

binary search: 1. left and right are 1 and n 2. while loop, assign middle being the midpoint 3. if it's the bad version, mid will be true and mid-1 will be false, so return mid 4. if not, check the right being mid-1 5. if it's not the bad version, make left=mid+1, traverse right

You are given an m x n integer array grid where grid[i][j] could be: 1 representing the starting square. There is exactly one starting square. 2 representing the ending square. There is exactly one ending square. 0 representing empty squares we can walk over. -1 representing obstacles that we cannot walk over. Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.

count number of empty squares, find the start dfs recursion: check if i and j are in the grid and not an obstacle check if the square is the end and e is not empty, assign square of visited, dfs for all neighboring squares, backtrack by assigning grid square to 0

format strings with f-strings

example: def get_name(name): return f"my name is {name} and Im {30}"

enumerate

iterate over list with access to both indices and the values

Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

pseudo code: 1. assigns 2 empty lists 2. goes thru each word in s, and adds the word's index in the string 3. checks if these strings are equal **works because string.index('a') returns the first index that has that letter**

Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array. Return the leftmost pivot index. If no such index exists, return -1.

pseudo code: 1. calculate total of array 2. right sum and left sum 3. for loop: right sum = total - current array value - left sum 4. check if leftsum=rightsum 5. otherwise return -1

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

slow version: class Solution: def sortedSquares(self, nums): return sorted([num * num for num in nums]) faster version in photo, 2 pointer: - On pointer i at start index, second pointer j at end index, a list ans of size n, and a k pointer to update value in ans list - compare abs value in nums where pointers are pointing - if abs(nums[l]>nums[r]: - update value at k position in ans with nums[l]* nums[l] - else: update value at k position in ans with nums[r]*nums[r]


Ensembles d'études connexes

Įžymybių industrija - Egzaminas

View Set

Lecture 12 - Exam 2 - Thoracic Spine Evaluation and Treatment

View Set

Biochemistry I Exam 3 (hunterschaff)

View Set

Transcultural Module 5.01: Globalization and Transcultural nursing

View Set

History 110 Final Study Guide (Chapters 14-27)

View Set

Professional Use of Electronic Media

View Set

Phoenix Fire Paramedic Test Study Guide

View Set