0% found this document useful (0 votes)
13 views4 pages

List

Uploaded by

Dinesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

List

Uploaded by

Dinesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Problem 1: Rotate List

Write a function rotate_list(lst, k) that rotates the elements of the list lst to the right by k positions.
The rotation should be in-place.

python

Copy code

def rotate_list(lst, k):

# Your code here

Example:

python

Copy code

rotate_list([1, 2, 3, 4, 5], 2) # Output: [4, 5, 1, 2, 3]

rotate_list([1, 2, 3], 4) # Output: [3, 1, 2]

Problem 2: Group Anagrams

Create a function group_anagrams(words) that takes a list of strings and groups the anagrams
together. Return a list of lists, where each inner list contains words that are anagrams of each other.

python

Copy code

def group_anagrams(words):

# Your code here

Example:

python

Copy code

group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])

# Output: [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

Problem 3: Maximum Subarray Sum

Write a function max_subarray_sum(lst) that finds the maximum sum of any contiguous subarray
within the list. You can use Kadane’s algorithm for this problem.

python

Copy code

def max_subarray_sum(lst):
# Your code here

Example:

python

Copy code

max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) # Output: 6 (subarray: [4, -1, 2, 1])

Problem 4: Merge Intervals

Create a function merge_intervals(intervals) that takes a list of intervals (each interval is a list of two
integers) and merges overlapping intervals. Return a new list of merged intervals.

python

Copy code

def merge_intervals(intervals):

# Your code here

Example:

python

Copy code

merge_intervals([[1, 3], [2, 6], [8, 10], [15, 18]])

# Output: [[1, 6], [8, 10], [15, 18]]

Problem 5: Find Missing Number

Write a function find_missing_number(lst, n) that finds the missing number in a list of integers from
1 to n. The list will contain n-1 integers.

Problem 1: Rotate List

You are given a list lst and an integer k. Rotate the list to the right by k positions in place.

python

Copy code

# Example

lst = [1, 2, 3, 4, 5]

k=2
# Your code here to rotate lst

# Output: [4, 5, 1, 2, 3]

Problem 2: Group Anagrams

Given a list of strings words, group the anagrams together in a new list of lists.

python

Copy code

# Example

words = ["eat", "tea", "tan", "ate", "nat", "bat"]

# Your code here to group anagrams

# Output: [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

Problem 3: Maximum Subarray Sum

You are given a list of integers lst. Find the maximum sum of any contiguous subarray using Kadane's
algorithm.

python

Copy code

# Example

lst = [-2, 1, -3, 4, -1, 2, 1, -5, 4]

# Your code here to find the maximum subarray sum

# Output: 6 (subarray: [4, -1, 2, 1])

Problem 4: Merge Intervals

You have a list of intervals, where each interval is represented as a list of two integers. Merge all
overlapping intervals and return the merged intervals.

python

Copy code

# Example

intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]


# Your code here to merge intervals

# Output: [[1, 6], [8, 10], [15, 18]]

Problem 5: Find Missing Number

Given a list of integers from 1 to n with one number missing, identify the missing number without
using any user-defined functions.

You might also like