Coding Set 3
Coding Set 3
Problem: Write a function that takes two sorted arrays and merges them into a single sorted
array without using any in-built sorting functions.
Sample Input:
Input: [1, 3, 5] and [2, 4, 6]
Sample Output:
Output: [1, 2, 3, 4, 5, 6]
Problem: Create a program that counts the number of vowels in a given string.
Sample Input:
Input: "OpenAI"
Sample Output:
Output: 3
(Explanation: The vowels are 'O', 'e', and 'A')
Problem: Write a function that performs a binary search on a sorted array to find the
position of a given target value.
Sample Input:
Input: [1, 2, 3, 4, 5], Target: 3
Sample Output:
Output: 2
(Explanation: The target value 3 is at index 2.)
Problem: Write a program to find the greatest common divisor (GCD) of two given integers
using the Euclidean algorithm.
Sample Input:
Input: 8, 12
Sample Output:
Output: 4
5. Remove Duplicates from a Linked List
Problem: Given a singly linked list, write a program to remove all duplicate values from the
list.
Sample Input:
Input: 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5
Sample Output:
Output: 1 -> 2 -> 3 -> 4 -> 5
Answers :
Python:
merged = []
i=j=0
merged.append(arr1[i])
i += 1
else:
merged.append(arr2[j])
j += 1
merged.extend(arr1[i:])
merged.extend(arr2[j:])
return merged
# Output: [1, 2, 3, 4, 5, 6]
JAVA :
import java.util.Arrays;
int i = 0, j = 0, k = 0;
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
merged[k++] = arr1[i++];
merged[k++] = arr2[j++];
return merged;
System.out.println(Arrays.toString(merge(arr1, arr2)));
// Output: [1, 2, 3, 4, 5, 6]
}
2. Count Vowels in a String
Python:
def count_vowels(s):
vowels = "aeiouAEIOU"
print(count_vowels("OpenAI"))
# Output: 3
JAVA :
int count = 0;
if (vowels.indexOf(c) != -1) {
count++;
return count;
System.out.println(countVowels("OpenAI"));
// Output: 3
}
}
Python:
if arr[mid] == target:
return mid
left = mid + 1
else:
right = mid - 1
return -1
# Output: 2
JAVA :
if (arr[mid] == target) {
return mid;
left = mid + 1;
} else {
right = mid - 1;
System.out.println(binarySearch(arr, 3));
// Output: 2
Python:
while b:
a, b = b, a % b
return a
print(gcd(8, 12))
# Output: 4
JAVA :
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
// Sample Input
int num1 = 8;
// Finding GCD
// Sample Output
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
}
5. Remove Duplicates from a Linked List
Python:
class ListNode:
self.val = val
self.next = next
def remove_duplicates(head):
current = head
while current:
runner = current
while runner.next:
if runner.next.val == current.val:
runner.next = runner.next.next
else:
runner = runner.next
current = current.next
return head
def print_list(node):
while node:
node = node.next
print("None")
remove_duplicates(head)
print_list(head)
import java.util.HashSet;
import java.util.LinkedList;
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(4);
list.add(4);
list.add(5);
System.out.println("Original list:");
System.out.println(list);
// Remove duplicates
removeDuplicates(list);
System.out.println(list);