Friday 22 November 2013

C# keywords VS Java keywords

Here is comparison between the keywords available in Java Language and C# Language:

Tuesday 19 November 2013

Calculation of Execution /Elapsed Time in Java

Two Java function can be use to calculate the elapsed time or execuiton time in java.

  1. System.currentTimeMillis()
  2. System.nanoTime()

Example:

System.currentTimeMillis()

 long StartTime = System.currentTimeMillis();
 //do some tasks
 long EndTime = System.currentTimeMillis();
 long difference = EndTime - StartTime;
 
 System.out.println("Elapsed milliseconds: " + difference);

Saturday 16 November 2013

Java Date And Calendar Examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar.

1. Java Date Examples

Few examples to work with Date APIs.

Example 1.1 – Convert Date to String.

 SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
 String date = sdf.format(new Date());
 System.out.println(date); //15/10/2013

Trick To View Private Profile Picture of Any User on Facebook

Facebook has been quick in adapting and fixing bugs. They have been rolling out too many updates in recent past and this has been helping users to find new bugs and tricks. Recently I was able to figure out the way to view profile picture of any user even if it is set to private using privacy settings. The trick isn’t difficult or requires any programming skill. It’s a simple url Hack.

Drawing Circle in Java Using 8-way Symmetry

Beginning with the equation the circle:
We can solve for 'y' in term of 'x':

and use this to compute the pixel of the circle.

Saturday 9 November 2013

Searching an Element in Rotated Sorted Array

One of my friend was ask to devise an algorithm to search a number on a rotated sorted array. Well, we can always do a linear search. But that cannot be optimal we are neglecting the fact that the array was once sorted. The array is still sorted but has been rotated at some point. So, if we could find the rotation point, we could still use binary search to find the number.

Playing with Big O - Complexity of different searching and sorting algorithm

While preparing for interview, I have wasted a lot of my time on summing down the best, average and worst case complexity of different searching and sorting algorithms. To save you guys from going through same pain, I am posting my work here. So, enjoy this cheat sheet while preparing for interview. 

Searching

Algorithm Data Structure Time Complexity Space Complexity
Average Worst Worst
Depth First Search (DFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Breadth First Search (BFS) Graph of |V| vertices and |E| edges - O(|E| + |V|) O(|V|)
Binary search Sorted array of n elements O(log(n)) O(log(n)) O(1)
Linear (Brute Force) Array O(n) O(n) O(1)

Always include the header files when using some standard C libraries

Yesterday, I was working on a C code of mine when I  came across a very peculiar problem. I even wrote a test code to regenerate the problem I was facing.

int main()
{
    char str[10]="3.5";
    printf("%lf",atof(str));
    return 0;
}
This is a simple code I am testing at ideone.com. I am getting the output as