Tuesday 28 October 2014

Collatz function

Difficulty: Easy

The Collatz function is defined for a positive integer n as follows.
f(n) = 3n+1 if n is odd
        n/2     if n is even

We consider the repeated application of the Collatz function starting with a given integer n, as follows:
f(n), f(f(n)), f(f(f(n))), …

It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260  [Wikipedia: Collatz Conjecture].

Sunday 19 October 2014

Count occurences of pattern string

Level: Medium

Given a source string S and a pattern string P, count the number of times the pattern string P occurs in the source string S. 
Note: Overlapping sequences are counted as separate occurrences. 

Input Format:
First line is the source string S s.t. 1 <= |S| <= 8192 characters
Second line is the pattern string P s.t. 1 <= |P| <= 8192 characters

Output Format:
Output a single integer containing the number of occurrences of pattern string P in source string S.

Print Subarray

Level: Easy

Given an input character array of A and start index S and end index E, write a function that prints the sub array starting from S (including S) and ending at index E (including E). The character array may contain spaces and tabs.
Note: You are given the main function. Just write the subroutine 'void printSubarray(char *a, int start, int end)'. 

Input Format:
First line is the input array A s.t. 1 <= |A| <= 8192
Second line is the start index S s.t. 0 <= S <= |A|-1
Third line is the ending index E s.t. 0 <= E <= |A|-1

Center Align Text

Level: Easy

Given an input string S, center justify the string by using the character '_' to align the string. There will be neither preceding spaces before the string S nor suffix spaces after the string S. The output should be center-justified in a line of width 64 characters, followed by a newline. 
Note:
1. If S has an odd number of characters then the number of preceding '_' should be one more than the number of trailing '_'
2. If S has an even number of characters then the number of preceding '_' should be equal to the number of trailing '_'

Input Format:
First line is the input string S s.t. 1 <= |S| <= 64

Wednesday 15 October 2014

Quiz on Array And Pointer


1 What is the output of the following program?

#include <stdio.h>

void foo(int a[])
{
a[0]=10;
printf("%d",a[0]);
return ;
}

int main()
{
int a[]={1,2,3};
foo(a);
printf("%d",a[0]);
return 0;
}
 
 
 
 
1 point

ans 1010