Dynamic Memory Allocation (Stack vs Heap)

Dynamic Memory Allocation

The Stack What is the stack? It’s a special region of your computer’s memory that stores temporary variables created by each function (including the main() function). The stack is a “LIFO” (last in, first out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, Read More →

Chickens and Rabbits | Math Puzzle

Puzzle: There are several chickens and rabbits in a cage (with no other types of animals). There are 72 heads and 200 feet inside the cage. How many chickens are there, and how many rabbits? Puzzle Solution: Let c be the number of chickens, and r be the number of rabbits. r + c = Read More →

Nugget Numbers | Math Brain Teaser

Puzzle: At McDonald’s you can order Chicken McNuggets in boxes of 6,9, and 20. What is the largest number of nuggets that you cannot order using any combination of the above? Puzzle Solution: 43 It is possible to achieve all multiples of 3 that are bigger than 6, through the 6s and 9s alone. Adding Read More →

Top 10 Brain Teasers

Ready to challenge your brain? Here’s our collection of brain teasers and the answers! Most people get these wrong. Try now these brain teasers! 1. Red and Blue Balls in a Bag You have 20 Blue balls and 10 Red balls in a bag. You put your hand in the bag and take off two Read More →

Frog and Well Puzzle

Puzzle: A frog is at the bottom of a 30 meter well. Each day he summons enough energy for one 3 meter leap up the well. Exhausted, he then hangs there for the rest of the day. At night, while he is asleep, he slips 2 meters backwards. How many days does it take him Read More →

House Robber | Dynamic Programming

Problem: You have n houses with certain amount of money stashed in each house. You can not steal any adjacent houses. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can steal. Solution: This is a simple dynamic programming problem. The key is Read More →

The Pill Problem | Hard Brain Teaser

Man and 3 Pill Puzzle

Puzzle: This is very hard brain teaser. A man has a medical condition that requires him to take two kinds of pills, call them P1 and P2. The man must take one P1 pill and one P2 pill each day, or he will die. If he takes more than 1 pill of the same kind Read More →

Rotate Matrix By 90 Degree

Problem description: Given an N X N integer matrix, rotate it bye 90 degrees in place.In-place means minimal extra memory to be used, i.e. don’t make a new array to copy into). Rotate clockwise means top-row becomes right-column, right column becomes bottom-row etc. eg. Solution: The idea is to do a “four-way” swap variable, we Read More →

Design of a Tinyurl Service

What is tinyurl? A tinyurl service compresses a long url into a short one. This saves space, and is useful for scenarios such as twitter or weibo, where each character counts. The down side is possible spam use. Concerns include longevity of a short url. i.e. “http://tiny.me/5ie0V2″. The highlight part can be any string with Read More →

Construct Binary Tree from Postorder and Inorder Traversal

Given postorder and inorder traversal of a tree, construct the binary tree. Solution: The post order array contains the elements in the order of post order traversal of the binary tree and we know that the last element in the post order traversal is the root of the binary tree. We can then search the Read More →

Construct Tree from given Preorder and Inorder Traversals

Given preorder and inorder traversal of a tree, construct the binary tree. Solution: The pre order array contains the elements in the order of pre order traversal of the binary tree and we know that the first element in the pre order traversal is the root of the binary tree. We can then search the Read More →

Combination Sum Problem

Problem: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2,…, ak) must Read More →