MODEL CODING QUESTIONS - IIIrd year
1. Find the Second Largest Element
Scenario: Find the second largest element in an array of integers.
Input: [12, 35, 1, 10, 34, 1]
Output: 34
2. Check for Duplicate Values
Scenario: Check if an array contains any duplicates.
Input: [1, 2, 3, 4, 5, 1]
Output: true
3. Left Rotate an Array by One
Scenario: Rotate an array left by 1 position.
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
4. Find the Missing Number
Scenario: Array contains numbers from 1 to n with one number missing. Find it.
Input: [1, 2, 4, 5, 6]
Output: 3
5. Sort 0s, 1s, and 2s
Scenario: Given an array of 0s, 1s, and 2s, sort them without using a sort method.
Input: [0, 2, 1, 2, 0]
Output: [0, 0, 1, 2, 2]
6. Maximum Subarray Sum (Kadane’s Algo)
Scenario: Find the maximum sum of a contiguous subarray.
Input: [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
7. Move All Zeroes to End
Scenario: Move all 0s to the end while maintaining the order of non-zero elements.
Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
8. Find Pairs With Given Sum
Scenario: Find all pairs in array that sum to a given number.
Input: arr = [1, 2, 3, 4, 5], target = 6
Output: (1,5), (2,4)
9. Count Frequency of Each Element
Scenario: Count how many times each element appears in an array.
Input: [2, 3, 2, 4, 4, 4]
Output: 2:2, 3:1, 4:3
10. Merge Two Sorted Arrays
Scenario: Merge two sorted arrays into one sorted array.
Input: [1, 3, 5], [2, 4, 6]
Output: [1, 2, 3, 4, 5, 6]
11. Find Peak Element
Scenario: An element is a peak if it is not smaller than its neighbors.
Input: [1, 3, 20, 4, 1, 0]
Output: 20
12. Subarray with Given Sum
Scenario: Check if there is a subarray with a given sum.
Input: arr = [1, 4, 20, 3, 10, 5], sum = 33
Output: true (subarray is [20, 3, 10])
13. Rearrange Positive and Negative Numbers Alternately
Input: [-1, 2, -3, 4, 5, 6, -7, 8]
Output: [2, -1, 4, -3, 5, -7, 6, 8]
14. Find Majority Element
Scenario: Find the element that appears more than n/2 times.
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
15. Check if Array is a Palindrome
Input: [1, 2, 3, 2, 1]
Output: true
16. Check for Anagram
Input: str1 = "listen", str2 = "silent"
Output: true
17. Reverse Words in a Sentence
Input: "Hello World Java"
Output: "Java World Hello"
18. Count Vowels and Consonants
Input: "Hello World"
Output: Vowels: 3, Consonants: 7
19. Check for Palindrome String
Input: "madam"
Output: true
20. Longest Repeating Character
Input: "aaabbcccddddee"
Output: d (4 times)
21. Remove Duplicates in String
Input: "programming"
Output: "progamin"
22. Find First Non-Repeating Character
Input: "swiss"
Output: w
23. Check if Two Strings are Rotations
Input: "ABCD", "CDAB"
Output: true
24. Compress a String (Basic Run-Length Encoding)
Input: "aaabbcddd"
Output: "a3b2c1d3"
25. Check if a String is a Pangram
Input: "The quick brown fox jumps over the lazy dog"
Output: true
26. Convert String to Integer
Input: "1234"
Output: 1234
27. Check if One String is Subsequence of Another
Input: s1 = "abc", s2 = "aebdc"
Output: true
28. Longest Common Prefix
Input: ["flower", "flow", "flight"]
Output: "fl"
29. Valid Parentheses
Input: "({[]})"
Output: true
30. Group Anagrams
Input: ["bat", "tab", "cat", "act"]
Output: [["bat", "tab"], ["cat", "act"]]
31. Minimum Swaps to Sort Array
Scenario: Find the minimum number of swaps required to sort an array.
Input: [4, 3, 2, 1]
Output: 2
Explanation: Swap 4 with 1, 3 with 2.
32. Find All Subarrays with 0 Sum
Scenario: Return all subarrays whose sum is 0.
Input: [6, 3, -1, -3, 4, -2, 2, 4, 6, -12, -7]
Output: Multiple subarrays like [3, -1, -3, 4, -2, -1], [6, 3, -1, -3, 4, -2, 2, 4, 6, -12]
33. Longest Substring Without Repeating Characters
Scenario: Find the longest substring with all distinct characters.
Input: "abcabcbb"
Output: 3
Explanation: "abc" is the longest.
34. Minimum Window Substring
Scenario: Find the smallest window in a string containing all characters of another
string.
Input: str1 = "ADOBECODEBANC", str2 = "ABC"
Output: "BANC"
35. Trapping Rain Water Problem
Scenario: Given elevation map, compute trapped rain water.
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
36. Sliding Window Maximum
Scenario: Find the max in every window of size k.
Input: [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
37. Longest Palindromic Substring
Scenario: Find the longest palindromic substring in a given string.
Input: "babad"
Output: "bab" or "aba"
38. Multiply Two Large Numbers (as Strings)
Scenario: Multiply two numbers given as strings (can be very large).
Input: "123", "456"
Output: "56088"
39. Check if One String Can Become Another by Swapping Two Letters
Scenario: Return true if string A can become B by swapping exactly one pair of
characters.
Input: A = "converse", B = "convesre"
Output: true
40. Longest Common Subsequence (LCS)
Scenario: Return length of LCS between two strings.
Input: s1 = "AGGTAB", s2 = "GXTXAYB"
Output: 4