value of expression
What is the value of 1/2 of 2/3 of 3/4 of 4/5 of 5/6 of 6/7 of 7/8 of 8/9 of 9/10 …. of 99/100 of 1000?
Missing and repeating number in an array
Given an unsorted array of size n with numbers ranging from 1 to n. One number from set {1, 2, 3, …, n} is missing and one number occurs twice in array. For example: If the size is 6, then array may be int arr[] = {1, 5, 3, 4, 1, 2}; Where 1 is […]
Singleton design pattern
Singleton patterns are used, when you want to allow creation of only one instance(object) of a particular class. Such classes (which allow only single object to be created) are called Singleton Classes. For example, there is only one Window manager, so a class implementing Windows Manager, should be allowed to be instantiated only once.
Memory leaks and dangling pointers
Those who use languages like C and C++ that support user defined pointer variables, put utmost emphasis on implementing the pointers correctly. And rightfully so, A pointer wrongly handled can be catastrophic. And that’s an understatement 🙂 .
Minimum Stack: Stack returning min element in O(1) time
Stack is a LIFO(Last-In-First-Out) list of elements where Push & Pop operations takes constant time, O(1). Design a Stack such that the operation of getminimum() (function returning minimum element of the stack) also takes constant time. Please note that it will only return the current minimum element from the stack and will not delete (pop) any […]
Find larger of 2 elements without branching
Given two unsigned integers, find the larger (or smaller) of the two without using branching (if-else or conditional operator).
One Definition Rule (ODR) in C++
One Definition Rule (ODR) In C++, declaration does not allocate any memory. Its only when an object (or function) is defined that an instance of it is created. The One definition Rule says that there cannot be more that one definition in a single translation unit of C++.
Continuous sub-array with given sum
Given an array of positive integers and a number ‘k’. Find continuous subarray whith sum of all the elements = k. For example.. Array k Output ——————— —— ————– {2, 4, 17, 9, 6, 3} 18 FOUND. arr[3] thru arr[5] {1, 0, 4, 0, 2} 7 FOUND. arr[0] thru arr[4] {1, 0, 4, 0, 2} […]
Check if two strings are anagrams
An anagram is a word play. If letters of a word can be arranged to form another word, the two words are called anagrams. e.g MARY & ARMY are anagrams because both of them are formed of characters A, M, R and Y.
Remove duplicates from a string
Given a string of characters. Write code which will remove duplicate characters from the string: Input: “social.ritambhara.in” Output: “social.rtmbhn”