# Category Archives: Algorithm

## Subtract two numbers without using arithmetic operatorsMarch 29, 2015

Write a function subtract(x, y) that returns x-y where x and y are integers. The function should not use any of the arithmetic operators (+,… Read More »

## Set Cover Problem | Set 1 (Greedy Approximate Algorithm)March 27, 2015

Given a universe U of n elements, a collection of subsets of U say S = {S1, S2…,Sm} where every subset Si has an associated… Read More »

## K Centers Problem | Set 1 (Greedy Approximate Algorithm)March 26, 2015

Given n cities and distances between every pair of cities, select k cities to place warehouses (or ATMs or Cloud Server) such that the maximum… Read More »

## Calculate square of a number without using *, / and pow()March 9, 2015

Given an integer n, calculate square of a number without using *, / and pow(). Examples: Input: n = 5 Output: 25

## Divide and Conquer | Set 7 (The Skyline Problem)March 8, 2015

Given n rectangular buildings in a 2-dimensional city, computes the skyline of these buildings, eliminating hidden lines. The main task is to view buildings from… Read More »

## What are the differences between bitwise and logical AND operators in C/C++?

A Bitwise And operator is represented as ‘&’ and a logical operator is represented as ‘&&’. Following are some basic differences between the two operators.

## How to print maximum number of A’s using given four keysMarch 6, 2015

This is a famous interview question asked in Google, Paytm and many other company interviews. Below is the problem statement.

## Divide and Conquer | Set 6 (Tiling Problem)March 4, 2015

Given a n by n board where n is of form 2k where k >= 1 (Basically n is a power of 2 with minimum… Read More »

## Find the minimum cost to reach destination using a trainMarch 2, 2015

There are N stations on route of a train. The train goes from station 0 to N-1. The ticket cost for all pair of stations… Read More »

## Write an iterative O(Log y) function for pow(x, y)March 1, 2015

Given an integer x and a positive number y, write a function that computes xy under following conditions. a) Time complexity of the function should… Read More »

## Vertex Cover Problem | Set 2 (Dynamic Programming Solution for Tree)February 28, 2015

A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either ‘u’… Read More »

## Bit Fields in CFebruary 27, 2015

In C, we can specify size (in bits) of structure and union members. The idea is to use memory efficiently when we know that the… Read More »

## Find position of an element in a sorted array of infinite numbersFebruary 22, 2015

Suppose you have a sorted array of infinite numbers, how would you search an element in the array? Source: Amazon Interview Experience.

## Count number of ways to reach a given score in a gameFebruary 17, 2015

Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of… Read More »

## An interesting time complexity questionFebruary 9, 2015

What is the time complexity of following function fun()?