Please 10 most frequently ask algorithm questions and answers in interview
- User
Please 10 most frequently ask algorithm questions and answers in interview
- Assistant
Please 10 most frequently ask algorithm questions and answers in interview
Certainly! Here are ten commonly-asked algorithm questions in coding interviews, along with brief explanations and solutions:
Problem:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Answer:
def twoSum(nums, target):
num_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
return []
Problem: Reverse a singly linked list.
Answer:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(head):
prev = None
current = head
while current:
next_temp = current.next
current.next = prev
prev = current
current = next_temp
return prev
Problem: Merge two sorted linked lists and return it as a new sorted list.
Answer:
def mergeTwoLists(l1, l2):
dummy = ListNode()
tail = dummy
while l1 and l2:
if l1.val < l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
tail.next = l1 or l2
return dummy.next
Problem: Given an array where the ith element is the price of a given stock on day i, find the maximum profit you can achieve.
Answer:
def maxProfit(prices):
min_price = float('inf')
max_profit = 0
for price in prices:
if price < min_price:
min_price = price
elif price - min_price > max_profit:
max_profit = price - min_price
return max_profit
Problem:
Given a string s, find the length of the longest substring without repeating characters.
Answer:
def lengthOfLongestSubstring(s):
char_map = {}
left = 0
max_length = 0
for right in range(len(s)):
if s[right] in char_map:
left = max(left, char_map[s[right]] + 1)
char_map[s[right]] = right
max_length = max(max_length, right - left + 1)
return max_length
Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
Answer:
def isValid(s):
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for char in s:
if char in mapping:
top_element = stack.pop() if stack else '#'
if mapping[char] != top_element:
return False
else:
stack.append(char)
return not stack
Problem: Given a sorted array that has been rotated, find a given target.
Answer:
def search(nums, target):
if not nums:
return -1
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
Problem: Given an integer array nums, find the contiguous subarray which has the largest sum.
Answer:
def maxSubArray(nums):
max_sum = nums[0]
current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
Problem: Find the minimum element in a rotated sorted array.
Answer:
def findMin(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]
Problem:
Given an array nums of n integers where n > 1, return an array such that answer[i] is equal to the product of all the elements of nums except nums[i].
Answer:
def productExceptSelf(nums):
length = len(nums)
answer = [1] * length
left = 1
for i in range(1, length):
left *= nums[i - 1]
answer[i] *= left
right = 1
for i in range(length - 2, -1, -1):
right *= nums[i + 1]
answer[i] *= right
return answer
These problems cover a wide range of common algorithms and data structures, and understanding these solutions will help you perform well in technical interviews.
Not yet included in a published release.