problems
problems
Table of Contents
Operators and Basics .............................................................................................................................. 1
Controlling Flow of Execution ............................................................................................................... 4
Arrays .................................................................................................................................................... 10
Functions ................................................................................................................................................ 19
Advanced Problems .............................................................................................................................. 20
8. Given two numbers, return true if the sum of both numbers is less than 100. Otherwise
return false.
A. Example
i. Input:
• Please enter first number: 44
• Please enter second number: 37
ii. Output:
• Sum less than 100: True
9. Write a program that takes three floating point numbers, the width and height of a rectangle, and
the radius of a circle and returns true if the rectangle can fit inside the circle.
A. Example
i. Input:
• Please enter width of rectangle: 8
• Please enter height of rectangle: 6
• Please enter radius of circle: 5
ii. Output:
• Rectangle fits inside circle: True
10. Write a program that takes a floating-point value in minutes and displays the corresponding
number if seconds as the closest integer value
A. Example
i. Input:
• Please enter minutes: 4.21
ii. Output:
• Time in seconds: 252
11. Write a program that takes a floating-point value in hours and displays the corresponding number
if seconds as the closest integer value
A. Example
i. Input:
• Please enter hours: 4.21
ii. Output:
• Time in seconds: 15156
12. Write a program that takes a floating-point number as input temperature in Fahrenheit and
converts it to Celsius.
A. Example
i. Input
• Please enter temperature in Fahrenheit: 89.6
ii. Output
• Temperature 89.6F is 32-degree Celsius
13. Write a program that takes a floating-point number as input temperature in Celsius and converts it
to Fahrenheit.
A. Example
i. Input
• Please enter temperature in Celsius: 32
ii. Output
• Temperature 32C is 89.6-degree Fahrenheit
14. Write a program for a farmer who wants to count the total number of legs of all the farm animals.
Assuming that the program works for three types of animals, chickens (2 legs), cows (4 legs) and
pigs (4 legs).
A. Example
i. Input
• Please enter number of chickens: 42
• Please enter number of cows: 2
• Please enter number of pigs: 4
ii. Output
• Total number of legs for the animals: 108
15. Write a program that asks the user for two floating point numbers (say a and b) and
prints out the value of (a + b) squared
A. Example
i. Input
• Please enter first number: 7.9
• Please enter second number: 2.1
ii. Output
• Square of (7.9 + 2.1) is 100
16. Write a program that asks the user for two floating point numbers (say a and b) and
prints out the value of (a3 + b3 + 3a2y + 3ab2)
A. Example
i. Input
• Please enter first number: 7.9
• Please enter second number: 2.1
ii. Output
• Result: 1000
17. Write a program that takes an integer number prints the square of the number.
A. Example
i. Input
• Please enter number: 8
ii. Output
• Square of 8 is 64
Arrays
42. Write a C++ program to find the largest element of a given array of integers
A. Example 1
i. Input
• Please enter array size: 7
• Please enter element 0: -9
• Please enter element 1: -5
• Please enter element 2: 2
• Please enter element 3: 10
• Please enter element 4: 6
• Please enter element 5: 7
• Please enter element 6: 8
ii. Output
• 10
43. Write a C++ program to find the most occurring element in an array of integers.
A. Example 1
i. Input
• Please enter array size: 7
• Please enter element 0: -9
• Please enter element 1: -5
• Please enter element 2: 2
• Please enter element 3: 10
• Please enter element 4: -9
• Please enter element 5: 7
• Please enter element 6: -9
ii. Output
• -9
44. Write a program to find missing elements in the set of numbers from 1 to 6, in a user given array
A. Example 1
i. Input
• Please enter array size: 10
• Please enter element 0: 2
• Please enter element 1: 4
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 6
• Please enter element 5: 6
• Please enter element 6: 1
• Please enter element 7: 1
• Please enter element 8: 4
• Please enter element 9: 2
ii. Output
• 3
• 5
45. Write a C++ program to separate even and odd numbers of an array of integers. Put all even
numbers first, and then odd numbers.
A. Example 1
i. Input
• Please enter array size: 10
• Please enter element 0: 2
• Please enter element 1: 5
• Please enter element 2: 9
• Please enter element 3: 10
• Please enter element 4: 16
• Please enter element 5: 16
• Please enter element 6: 18
• Please enter element 7: 7
• Please enter element 8: 3
• Please enter element 9: 2
ii. Output
• [2, 10, 16, 16, 18, 2, 5, 9, 7, 3]
46. Write a program that breaks an integer down to its corresponding digits and prints the digits in an
array. Assume a maximum integer 99999
A. Example 1
i. Input
• Please enter integer: 95923
ii. Output
• [9, 5, 9, 2, 3]
47. Write a C++ program to count the number of occurrences of given number in an Unsorted array
of integers.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter number to search for: 6
ii. Output
• 3
48. Write a C++ program to count the number of occurrences of given number in a sorted array of
integers.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 4
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 6
• Please enter element 5: 6
• Please enter number to search for: 6
ii. Output
• 3
49. Write a C++ program to find and print all unique elements of a given array of integers.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
ii. Output
• [2, 6, 4]
50. Given an array of integers and a value, remove all instances of that value and return the new
length.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter number to remove: 4
ii. Output
• 4
51. Given a sorted array, remove the duplicates such that each element appears only once.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 4
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 6
• Please enter element 5: 6
ii. Output
• [2, 4, 6]
52. Write a program to find the cumulative sum of a given array.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
ii. Output
• [2, 8, 12, 18, 22, 28]
53. Write a program to find the reverse cumulative sum of a given array
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
ii. Output
• [28, 26, 20, 16, 10, 6]
54. Write a program to search for an element in an array and print the index of first occurrence.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter the element to search for: 4
ii. Output
• 2
55. Insert an element in an array at given index and print new array.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter element to be inserted: 5
• Please enter index to be inserted at: 3
ii. Output
• [2, 6, 4, 5, 6, 4, 6]
56. Rotate a matrix clockwise
57. Transpose a matrix
58. Given an integer array, find the contiguous subarray (containing at least one number) which has
the largest sum and return its sum.
A. Example:
i. Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4],
ii. Output: 6
• Explanation: [4, -1, 2, 1] has the largest sum = 6.
59. Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral
order.
A. Example:
i. Input:
• [
• [ 1, 2, 3 ],
• [ 4, 5, 6 ],
• [ 7, 8, 9 ]
• ]
ii. Output: [1,2,3,6,9,8,7,4,5]
60. Given a set of distinct integers, return all possible subsets (the power set). Note: The solution set
must not contain duplicate subsets.
A. Example:
i. Input:
• [1,2,3]
ii. Output:
• [
• [3],
• [1],
• [2],
• [1,2,3],
• [1,3],
• [2,3],
• [1,2],
• []
• ]
61. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and
return its area.
A. Example:
i. Input:
• [
• [1, 0, 1, 0, 0],
• [1,0, 1, 1, 1],
• [1, 1, 1, 1, 1],
• [1, 0, 0, 1, 0]
• ]
ii. Output: 6
62. Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's
triangle, each number is the sum of the two numbers directly above it.
A. Example:
i. Input: 5
ii. Output:
• [
• [1],
• [1,1],
• [1,2,1],
• [1,3,3,1],
• [1,4,6,4,1]
• ]
63. Given an array, write a function to move all 0's to the end of it while maintaining the relative order of
the non-zero elements.
A. Example:
i. Input: [0,1,0,3,12]
ii. Output: [1,3,12,0,0]
64. Given a non-empty array of integers, return the third maximum number in this array. If it does not
exist, return the maximum number.
A. Example 1:
i. Input: [3, 2, 1]
ii. Output: 1
• Explanation: The third maximum is 1.
B. Example 2:
i. Input: [1, 2]
ii. Output: 2
• Explanation: The third maximum does not exist, so the maximum (2) is
returned instead.
C. Example 3:
i. Input: [2, 2, 3, 1]
ii. Output: 1
• Explanation: Note that the third maximum here means the third
maximum distinct number. Both numbers with value 2 are both
considered as second maximum.
65. Given a binary array, find the maximum number of consecutive 1s in this array.
A. Example 1:
i. Input: [1, 1, 0, 1, 1, 1]
ii. Output: 3
• Explanation: The first two digits or the last three digits are consecutive
1s. The maximum number of consecutive 1s is 3.
66. Partition array into two separate arrays, given index of partition
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter index of partition: 2
ii. Output
• [2, 6]
• [4, 6, 4, 6]
67. Print out nested array. A zero-indexed array A of length N contains all integers from 0 to N-1.
Print the array as {A[i], A[A[i]], A[A[A[i]]], ... } for 10 steps.
A. Example:
i. Input: A = [5,4,0,3,1,6,2]
ii. Output: {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
68. Write a program that asks the user for an integer pivot, and partitions an array such that all
elements smaller than pivot are in one array, while all elements larger or equal to pivot are in
another array.
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 7
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 3
• Please enter pivot: 5
ii. Output
• [2, 4, 4, 3]
• [7, 6]
69. Write a program to filter out elements from an array larger than user input number
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter filter value: 3
ii. Output
• [6, 4, 6, 4, 6]
70. Write a program to add user input value to every element in array
A. Example 1
i. Input
• Please enter array size: 6
• Please enter element 0: 2
• Please enter element 1: 6
• Please enter element 2: 4
• Please enter element 3: 6
• Please enter element 4: 4
• Please enter element 5: 6
• Please enter element to be added: 3
ii. Output
• [5, 9, 7, 9, 7, 9]
71. Write a program to generate Collatz sequence for user given number.
72. Write a program to add two user input matrices.
73. Write a program to subtract two user input matrices.
74. Write a program to element wise divide two matrices.
75. Write a program to print the first 20 Fibonacci numbers in reverse.
76. Write a program to concatenate two arrays.
77. Write a program to concatenate two matrices along x axis.
78. Write a program to save diagonal of matrix as an array.
79. A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now
given an M x N matrix, print True if and only if the matrix is Toeplitz.
A. Example 1:
i. Input:
• matrix = [
• [1, 2, 3, 4],
• [5, 1, 2, 3],
• [9, 5, 1, 2]
• ]
ii. Output: True
• Explanation:
• In the above grid, the diagonals are:
• "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". In each diagonal all
elements are the same, so the answer is True.
80. A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row,
column, and both diagonals all have the same sum. Check if a 3 x 3 matrix is a magic square.
A. Example 1:
i. Input:
• matrix = [
• [4, 3, 8],
• [9, 5, 1],
• [2, 7, 6]
• ]
ii. Output: True
81. In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.
There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such
that the distance between him and the closest person to him is maximized. Return that maximum
distance to closest person.
A. Example 1:
i. Input: [1, 0, 0, 0, 1, 0, 1]
ii. Output: 2
• Explanation:
• If Alex sits in the second open seat (seats[2]), then the closest person has
distance 2.
• If Alex sits in any other open seat, the closest person has distance 1.
• Thus, the maximum distance to the closest person is 2.
82. Find array advantage. Given two arrays A and B of equal size, the advantage of A with respect to
B is the number of indices i for which A[i] > B[i].
83. Given a fixed length array of integers, duplicate each occurrence of zero, shifting the remaining
elements to the right. Note that elements beyond the length of the original array are not written.
A. Example 1:
i. Input: [1, 0, 2, 3, 0, 4, 5, 0]
ii. Output:[1, 0, 0, 2, 3, 0, 0, 4]
84. Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that
occurs more than 25% of the time. Return that integer.
A. Example 1:
i. Input: [1, 2, 2, 6, 6, 6, 6, 7, 10]
ii. Output: 6
85. Given a date, return the corresponding day of the week for that date. The input is given as three
integers representing the day, month and year respectively. Return the answer as one of the
following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"}.
A. Example 1:
i. Input: day = 31, month = 8, year = 2019
ii. Output: "Saturday"
86. You are given an array of coordinates, coordinates[i] = [x, y], where [x, y] represents the
coordinate of a point. Check if these points make a straight line in the XY plane.
A. Example 1:
i. Input: coordinates = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
ii. Output: True
87. Write a program to find all odd elements in a matrix.
88. Write a program to row wise rotate matrix by k elements.
89. Given a matrix representing a Tic Tac Toe game, find Winner on a Tic Tac Toe Game
90. Given an array of integers, return how many of them contain an even number of digits.
A. Example 1:
i. Input: [12, 345, 2, 6, 7896]
ii. Output: 2
• Explanation:
• 12 contains 2 digits (even number of digits).
• 345 contains 3 digits (odd number of digits).
• 2 contains 1 digit (odd number of digits).
• 6 contains 1 digit (odd number of digits).
• 7896 contains 4 digits (even number of digits).
• Therefore only 12 and 7896 contain an even number of digits.
91. Given an array of integers, check if there exists two integers N and M such that N is the double of
M ( i.e. N = 2 * M). More formally check if there exists two indices i and j such that :
o i != j
o 0 <= i, j < arr.length
o arr[i] == 2 * arr[j]
A. Example 1:
i. Input: arr = [10, 2, 5, 3]
ii. Output: true
• Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
92. Given the array nums, for each nums[i] find out how many numbers in the array are smaller than
it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j]
< nums[i]. Return the answer in an array.
A. Example 1:
i. Input: nums = [8, 1, 2, 2, 3]
ii. Output: [4, 0, 1, 1, 3]
• Explanation:
• For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
• For nums[1]=1 does not exist any smaller number than it.
• For nums[2]=2 there exist one smaller number than it (1).
• For nums[3]=2 there exist one smaller number than it (1).
• For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
Functions
93. Write a function to find the square of any number.
A. Example :
i. Input any number for square : 20
ii. Expected Output : The square of 20 is : 400.00
94. Write functions for the following operations.
• insert(val): Inserts an item val to the set if not already present.
• remove(val): Removes an item val from the set if present.
• getRandom: Returns a random element from current set of elements.
95. Write a function to swap two numbers.
A. Example :
i. Input 1st number : 2
ii. Input 2nd number : 4
iii. Expected Output :
• Before swapping: n1 = 2, n2 = 4
• After swapping: n1 = 4, n2 = 2
96. Write a function to check a given number is even or odd.
A. Example :
i. Input any number : 5
ii. Expected Output : The entered number is odd.
97. Write a function to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5+ ... +N!/N.
A. Example:
i. Input: 5
ii. Expected Output : The sum of the series is : 34
• Explanation: sum of 1!/1+2!/2+3!/3+4!/4+5!/5
98. Write a function to check whether a number is a prime number or not.
A. Example :
i. Input a positive number : 5
ii. Expected Output : The number 5 is a prime number.
99. Write a function to check Armstrong and perfect numbers.
A. Example :
i. Input any number: 371
ii. Expected Output :
iii. The 371 is an Armstrong number.
iv. The 371 is not a Perfect number.
100. Write a function to print all perfect numbers in given range.
A. Example :
i. Input lowest search limit of perfect numbers : 1
ii. Input lowest search limit of perfect numbers : 100
iii. Expected Output : The perfect numbers between 1 to 100 are : 6 28
101. Write a function that takes an integer number as a parameter and returns the resultant binary
number.
A. Example
i. Input
• Please enter number: 9
ii. Output
• Binary equivalent of 9 is: 1001
Advanced Problems
1. Sudoku: Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to
fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 sub-grids that
compose the grid (also called "boxes", "blocks", or "regions") contain all of the digits from 1 to 9.
[source: Wiki]
A. Part 1: Write a function for a sudoku puzzle setter that generates random legitimate
(solvable) sudoku puzzles and saves the puzzle in a file.
B. Part 2: Write a function for a sudoku puzzle solver that takes a sudoku puzzle file and
prints out the solved puzzle.
2. File System: Write a program which simulates a file system for text files.
Your program should be able to:
A. Add new text files and remove existing files in the system
B. Display list of files in your system and other metadata as needed, such as
i. Name of the file
ii. Size of the file
iii. Initial add date
iv. Last open date
v. And other information etc.
C. Display contents of existing files
Your program would maintain a single database-esque file that contains data for all the files as
well as a data structure (stored in the same database file) which maintains records of the starting
and end location of each file and other metadata as needed.