1. Write a program to find whether the given number is Armstrong number or not.
Armstrong Number is a number that is equal to the sum of cubes of its digits
E.g. 153, 370,371 etc
2. Write a program to find first non repeated element in an integer array
Input array [1,2,4,5,1,3,2,4,3,6,7,9,1]
Output = 5
3. Write a program to count the occurrences of a character in a given string.
Input String: DESKNINE
Input character-E
Output-2
4. Print the below pattern
1
12
123
1234
12345
1234
123
12
5. Write a program to sort an integer array without using inbuilt functions
Note: First half of the array should be sorted in ascending order where in second half should be sorted
in descending order.
E.g. Input array =[4,9,3,5,1,8,7,2] Output array [3,4,5,9,8,7,2,1]
6. Write a program to find the binary value of an integer
Input: 4
Output = 0100
Input: 8
Output = 1000
7. Write a program to find the missing integers in an array where 1to 100 numbers are stored.
[Link] the functions -FirstReverse(str) take the str parameter being passed and return the string in
reversed order
Example
Input - coderbyte
Output - etybredoc
Input - l Love Code
Output - edoC evoL I
9) “Palindrome Two”.
Problem: Have the function PalindromeTwo(str) take the str parameter being passed and return the
string true if the parameter is a palindrome, (the string is the same forward as it is backward)
otherwise return the string false. The parameter entered may have punctuation and symbols but they
should not affect whether the string is in fact a palindrome. For example: “Anne, I vote more cars race
Rome-to-Vienna” should return true.
10) Have the function Division (num1, num2) take both parameters being passed and return the
Greatest Common Factor. That is, return the greatest number that evenly goes into both numbers with
no remainder. For example: 12 and 16 both are divisible by 1, 2. and 4 so the output should be 4. The
range for both parameters will be from 1 to 10^3
Examples
Input: 7& 13
Output: 1
Input: 36 & 54
Output: 18
11) Flood Fill
Problem:
Given an m x n 2D array image representing an image, and three integers sr, sc, and
newColor, flood fill the image starting from the pixel (sr, sc). Replace the color of the
pixel and all adjacent pixels of the same color with newColor.
Solution:
Use depth-first search (DFS) or breadth-first search (BFS) to recursively replace colors.
Example:
Input:
image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
Output:
[[2,2,2],[2,2,0],[2,0,1]]
12) Implement Stack Using Two Queues
Problem:Implement a stack using two queues. Support the following operations:
push(x): Push an element onto the stack.
pop(): Remove the element on top of the stack.
top(): Get the top element.
empty(): Return whether the stack is empty.
Solution:
Maintain two queues and use them to reverse the order of elements to mimic stack behavior.
Example:
Input:
push(1)
push(2)
top() -> 2
pop() -> 2 empty() -> false
13. Merge Intervals
Problem:
Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals and
return an array of the non-overlapping intervals.
Solution:
Sort intervals by start time and merge overlapping intervals.
Example:
Input:
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
Output: [[1, 6], [8, 10], [15, 18]]
Explanation: Intervals [1, 3] and [2, 6] overlap and are merged into [1, 6].
14. Number of Islands
Problem:
Given an m x n grid of 1s (land) and 0s (water), count the number of islands. An island is surrounded
by water and formed by connecting adjacent lands horizontally or vertically.
Solution:
Use DFS to mark all connected 1s as visited.
Example:
Input: grid = [ ["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
Output: 1
15. Find Top K Frequent Elements
Problem:
Given an integer array nums and an integer k, return the k most frequent elements.
You may return the answer in any order.
Solution:
Use a HashMap to store the frequency of each element, then use a priority queue (min-heap) to extract
the top k frequent elements.
Example:
Input: nums = [1, 1, 1, 2, 2, 3], k = 2
Output: [1, 2]
Explanation:
1 appears 3 times, 2 appears 2 times, and 3 appears once.
The top 2 frequent elements are 1 and 2.
16. Sort a List of Products by Price and Name
Problem:
Sort a list of products first by their prices (ascending), and if two products have the same price, sort
them alphabetically by their names.
Solution:
Use the Comparable interface in the Product class. Override the compareTo method to implement the
required sorting logic.
Example:
Input: [Product("Laptop", 1200), Product("Mouse", 25), Product("Keyboard", 25),
Product("Monitor", 200)]
Output: [Product("Keyboard", 25), Product("Mouse", 25), Product("Monitor", 200),
Product("Laptop", 1200)]
17)
18)