an HCL GUVI product

11. Check If a Given Array Represents a Min Heap

Required Input:

arr = [1, 3, 5, 7, 9, 11, 15]

Expected Output:

True

Code In Python

def is_min_heap(arr): # Write your logic here pass # Prefilled input arr = [1, 3, 5, 7, 9, 11, 15] print(is_min_heap(arr))

Run Code?

Click Run Button to view compiled output

12. Group Anagrams Using a HashMap

Required Input:

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

Expected Output:

[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

Code In Python

from collections import defaultdict def group_anagrams(words): # Write your logic here pass # Prefilled input words = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'] print(group_anagrams(words))

Run Code?

Click Run Button to view compiled output

13. Find Two Numbers That Sum Up to a Target Using a HashMap

Required Input:

arr = [2, 7, 11, 15] target = 9

Expected Output:

[0, 1]

Code In Python

def two_sum(arr, target): # Write your logic here pass # Prefilled input arr = [2, 7, 11, 15] target = 9 print(two_sum(arr, target))

Run Code?

Click Run Button to view compiled output

14. Find the Intersection of Two Arrays Using a HashMap

Required Input:

arr1 = [1, 2, 2, 1] arr2 = [2, 2]

Expected Output:

[2]

Code In Python

def array_intersection(arr1, arr2): # Write your logic here pass # Prefilled input arr1 = [1, 2, 2, 1] arr2 = [2, 2] print(array_intersection(arr1, arr2))

Run Code?

Click Run Button to view compiled output

15. Find the Lowest Common Ancestor (LCA) of Two Nodes in a BST

Required Input:

root = [6, 2, 8, 0, 4, 7, 9, None, None, 3, 5] p = 2 q = 8

Expected Output:

6

Code In Python

def lowest_common_ancestor(root, p, q): # Write your logic here pass # Prefilled input root = TreeNode(6, TreeNode(2, TreeNode(0), TreeNode(4, TreeNode(3), TreeNode(5))), TreeNode(8, TreeNode(7), TreeNode(9))) p = 2 q = 8 print(lowest_common_ancestor(root, p, q))

Run Code?

Click Run Button to view compiled output

16. Convert a Sorted Array to a Balanced BST

Required Input:

nums = [-10, -3, 0, 5, 9]

Expected Output:

BST with root 0, left child -3, right child 9.

Code In Python

def sorted_array_to_bst(nums): # Write your logic here pass # Prefilled input nums = [-10, -3, 0, 5, 9] print(sorted_array_to_bst(nums))

Run Code?

Click Run Button to view compiled output

17. Merge K Sorted Arrays Using a Min Heap

Required Input:

arrays = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Expected Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Code In Python

import heapq def merge_k_sorted_arrays(arrays): # Write your logic here pass # Prefilled input arrays = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] print(merge_k_sorted_arrays(arrays))

Run Code?

Click Run Button to view compiled output

18. Find the Kth Largest Element in a Stream

Required Input:

k = 3 stream = [4, 5, 8, 2] operations = ['add 3', 'add 5', 'add 10', 'add 9', 'add 4', 'get_kth_largest']

Expected Output:

8

Code In Python

import heapq class KthLargest: def __init__(self, k, nums): # Write your logic here pass def add(self, val): pass def get_kth_largest(self): pass # Prefilled input k = 3 stream = [4, 5, 8, 2] obj = KthLargest(k, stream) operations = ['add 3', 'add 5', 'add 10', 'add 9', 'add 4', 'get_kth_largest'] for op in operations: if 'add' in op: obj.add(int(op.split()[1])) elif op == 'get_kth_largest': print(obj.get_kth_largest())

Run Code?

Click Run Button to view compiled output

19. Find the Longest Substring Without Repeating Characters Using a HashMap

Required Input:

s = 'abcabcbb'

Expected Output:

3

Code In Python

def longest_unique_substring(s): # Write your logic here pass # Prefilled input s = 'abcabcbb' print(longest_unique_substring(s))

Run Code?

Click Run Button to view compiled output

20. Design an LRU Cache Using a HashMap and Doubly Linked List

Required Input:

capacity = 2 operations = ['put 1 1', 'put 2 2', 'get 1', 'put 3 3', 'get 2', 'put 4 4', 'get 1', 'get 3', 'get 4']

Expected Output:

1
-1
-1
3
4

Code In Python

lass LRUCache: def __init__(self, capacity): # Write your logic here pass def get(self, key): pass def put(self, key, value): pass # Prefilled input cache = LRUCache(2) operations = ['put 1 1', 'put 2 2', 'get 1', 'put 3 3', 'get 2', 'put 4 4', 'get 1', 'get 3', 'get 4'] for op in operations: if 'put' in op: key, value = map(int, op.split()[1:]) cache.put(key, value) elif 'get' in op: key = int(op.split()[1]) print(cache.get(key))

Run Code?

Click Run Button to view compiled output

2 of 3