Fundamentals Quiz
Easy Difficulty (10 Questions)
1. You are given a sorted array of numbers. You need to find two numbers that can be
added to achieve a sum equal to a specific target. If no such combination is present,
simply print a relevant message.
Example:
numbersArray = [2,5,6,12,23]
target = 28
Answer: 5, 23
2. Strings or numbers that are read the same backwards and forwards are called
palindromes. For example: madam, racecar.
You’re given a string and you need to figure out if the given string is a palindrome or not.
Simply print the relevant message.
Example:
inputString = ‘hello’ Result: false
inputString = ‘civic’ Result: true
3. URLify: Write a method to replace all spaces in a string with '%20' . You may assume
that the string has sufficient space at the end to hold the additional characters, and that
inputString.length will return you the correct length of the input string.
Example:
inputString = ‘My Name is John’ Output = ‘My%20Name%20is%20John’’
4. Given a number, write the code to reverse and print that number.
Example:
inputNumber = 251, Output = 152
5. Given a string, count the number of vowels in it. (Vowels are: a, e, i, o u)
Example:
inputString = “Hello, World”
Output = 3
6. Given a 2D-Array containing just 0 and 1, count the number of 1s present.
7. Given a number, write code to check if it is a prime number. Simply print a relevant
message. (Prime numbers are the numbers which can not be divided by any other
number, except 1 and the number itself e.g 7, 13, etc.)
8. Given a number, calculate its factorial using recursion. Factorial is the product of all
positive integers less than or equal to a given positive integer and denoted by that
integer and an exclamation point. Thus, factorial seven is written 7!
7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040
Which also means that:
7! = 7 x 6! = 5040
Because
6! = 6 x 5 x 4 x 3 x 2 x 1
Example:
inputNumber = 5
Output = 120
9. Given a linked list, write code to reach the middle node. Simply print the value of data in
the middle node.
Example:
inputLinkedList = 2 -> 5 -> 9 -> 4 -> 3, Output: 9
inputLinkedList = 10 -> 2 -> 4 -> 1, Output: 4
10. Given a linked list of length more than or equal to 3, write code to remove the second
node of the linked list.
Example:
inputLinkedList = 2 -> 5 -> 9 -> 4 -> 3, Output: 2 -> 9 -> 4 -> 3
inputLinkedList = 10 -> 2 -> 4 Output: 10 -> 4
Medium/Hard Difficulty (10 Questions)
1. Given a sequence of brackets, determine if it is a valid sequence. There must be a valid
closing bracket present for each opening bracket and the closing bracket must be on the
right index/location.
Hint: Use Stack
Example:
inputString = [ { ( ) } () ] Output = true
inputString = [ { ( } ) () ]
2. Given a number k and a linked list, write code to print the value of the kth index of the
linked list.
Example:
inputLinkedList = 2 -> 3 -> 11 -> 7-> 0 -> 22, k=2
Output = 11
3. Given a binary tree, write code to check if it is a valid Binary Search Tree.
4. Implement a method to perform basic string compression using the counts of repeated
characters. For example, the string aabcccccaaa would become a2b1c5a3. If the
"compressed "string would not become smaller than the original string, your method
should return the original string. You can assume the string has only uppercase and
lowercase letters (a - z),
5. Given a linked list, write code to remove duplicates from it.
Example:
inputLinkedList = 2 -> 10 -> 5 -> 24 -> 76 -> 5 -> 2 -> 99
Output = 2 -> 10 -> 5 -> 24 -> 76 -> 99
6. Given a sorted array, implement Binary Search to find a target value. If the target
number is not present, simply return -1 or print a relevant message.
Example:
numbersArray = [2,6,8,10,17,21,22], target=17
Output = 4
7. Given a linked list, determine if it has a loop. Return true or false and print a relevant
message.
8. You are given two linked lists of variable lengths. Each list contains a number in the
following way:
listOne = 2 -> 4 -> 5 means that the first number is 542
listTwo = 9 -> 1 -> 1 -> 4 means that the second number is 4119
Determine the numbers from both the input linked lists, add them and store them in a
new linked list in the same way e.g. sum of 542 and 4119 is 4661.
So, the output will be 1 -> 6 -> 6 -> 4
9. Partition; Write code to partition a linked list around a value x, such that all nodes less
than x come before all nodes greater than or equal to x. If x is contained within the list,
the values of x only need to be after the elements less than x (see below). The partition
element x can appear anywhere in the "right partition"; it does not need to appear
between the left and right partitions.
Example:
input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1, partition = 5
Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
10. Given a Binary Search Tree, write code of its in-order traversal.
In-order traversal works like this:
- Traverse the left-child / left sub-tree
- Traverse the root
- Traverse the right-child / right sub-tree