/* Ajith - Syntax Higlighter - End ----------------------------------------------- */

## Pages

Showing posts with label Interview Question. Show all posts
Showing posts with label Interview Question. Show all posts

## 3.14.2013

### Divide a number by 3 without using any arithmetic operators

How to divide a number by 3 without using operators + - / * %

NOTE: I am just trying to collect various answers down in this post available across multiple sites in internet as mentioned in references.

Solution 01:
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp=fopen("temp.dat","w+b");
int number=12346;
int divisor=3;
char *buf = calloc(number,1);
fwrite(buf,number,1,fp);
rewind(fp);
printf("%d / %d = %d", number, divisor, result);
free(buf);
fclose(fp);
return 0;
}

## 3.13.2013

### Checking Balance of Symbols in a expression

A balanced expression contains right number of closing and open braces.

For example:

• [[ - unbalanced expression
• []{}() - balanced expression
• [(*)] - balanced expression

Let us see how to find if an expression is balanced or not by checking for following operators [ ] { } and ( ) in the given expression.

## 2.05.2013

### Converting a Fully Parenthesized Infix expression into Postfix expression

#### Analysis:

Five types of input characters

• Opening parentheses
• Operands
• Operators
• Closing parentheses
• New line character (\n)

#### Pseudocode:

********************************************************
Function  : main

Calls     : createStack
freeStack
pushIntoStack
popFromStack

Called by : NONE

Input
Parameters: Accepts input expression for processing

Returns   : Converts Fully paranthesized INFIX
expression into POSTFIX expression
********************************************************

SET i to 0
GET infix expression from user into input_array
SET var with input_array[i]

CALL createStack

WHILE var != end of string

IF var equals to '(' THEN

CALL pushIntoStack (stack, var)

ELSE IF var is a number THEN

PRINT var

ELSE IF var is an arithmetic operator THEN

CALL pushIntoStack (stack, var)

ELSE IF var equals to ')' THEN

WHILE stackTop != '('

IF stackTop is an arithmetic operator THEN

PRINT stackTop
popFromStack (stack)

ENDIF

ENDWHILE

popFromStack (stack)

ENDIF

SET var with input_expression[INCREMENT i]

ENDWHILE

CALL freeStack (stack)

## 12.25.2012

### Detecting First Node in a Loop in the List

Previous Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.
Next Article: Finding Nth node from end of a Singly Linked List.

Once we confirm that there is a Loop in a Singly Linked List we will see how to determine first node of the loop.

## 11.14.2012

### Finding Nth node from end of a Singly Linked List

Previous Article: Finding first node in a Loop in Singly Linked List.

 Figure 1: Singly Linked List

Solution 01 - Brute Force Approach:
1. Start at First Node of the List (call it curNodePtr).
2. Assign curNodePtr to tmpPtr and count number of nodes after the curNodePtr.
3. If number of nodes after curNodePtr are equal to N nodes or tmpPtr reaches END then break. If tmPtr reaches END but count not equal to N then return since we can't find the Nth node from the end of the Singly Linked List.
4. Move the curNodePtr one step forward in the Linked List i.e curNodePtr now points to its next node in the list and start again from STEP-2.

## 11.07.2012

### Reversing a Singly Linked List

Previous Article: Deleting a Node from a Singly Linked List.
Next Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.

Let us see how to reverse a Singly Linked List.

 Figure-1: Singly Linked List
Pseudocode:
cur_ptr = HEAD->NEXT
prev_ptr = NULL

forever:

if cur_ptr == NULL
break

tmp_ptr  = prev_ptr
prev_ptr = cur_ptr
cur_ptr  = cur_ptr->NEXT

prev_ptr->NEXT = tmp_ptr

HEAD->NEXT = prev_ptr
Complexity:
Time Complexity: O(n)
Space Complexity: O(3)

If we try on the example in Figure-1 we get the output as shown below

 Figure-2: After reversing the Singly Linked List

Previous Article: Deleting a Node from a Singly Linked List.
Next Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.

## 9.22.2012

### Detecting a Loop in Singly Linked List - Tortoise & Hare

Previous Article: Reversing a Singly Linked List.
Next Article: Finding first node in a Loop in Singly Linked List.

Eventhough there are multiple algorithms available we start with

Floyd's Cycle-Finding Algorithm
In simple terms it is also known as "Tortoise and Hare Algorithm" or "Floyd's Cycle Detection Algorithm" named after its inventor Robert Floyd. It is one of the simple cycle detection algorithm. It's a simple pointers based approach.

 Robert Floyd

## 8.23.2012

### Write a C Program without main function

In a C program main function is the entry point so it is mandatory to have a main function.

But let us see how can we write a program without main (Kind of hiding main in some obfuscated code). This post is purely out of interest to know that we can do something weird like this, just for learning.

## 8.09.2012

### Implementing ls command in C

We know that ls command in unix displays the list of files in a directory. It has various options to display the data in various styles and formats.

By default its implementation is part of Coreutils package, which is default package in all Linux flavours.

I want to see how we can implement its basic functionality with a simple C program.