Archive for the ‘Uncategorized’ Category.

Find the number of rotations performed on a sorted array

Given a sorted array rotated n times. Find the number of right side rotations performed on the array.
Example:
input: 678912345
expected output: 4 (rotating 123456789 4 times we will get the input)

Find the smallest window that covers a set of words

Given a document and a query of K words, how do u find the smallest window that covers all the words at least once in that document?

Find Intersection point of 2 linked lists

Given 2 linked lists merged at some point in the form of a ‘Y’. Find the intersection point

Search for files with phone numbers

You have 50,000 html files, some of which contain phone numbers. How would you create a list of all the files which contain phone numbers?
Note : Phone numbers are in format ddd[- OR ' ']ddd[- OR ' ']dddd

Singleton Design Pattern implementation in Java

  • Ensure a class has only one instance and provides a global point of access to it
  • Useful when exactly one instance of a class is needed to coordinate actions
  • Abstract Factory,Prototype and Builder can use Singleton in their implementation
  • Facade objects and State objects are often SIngletons
  • Often preffered as global variables -Provides OO Design scope,Allows lazy initialization or dynamic change in behaviour

Continue reading ‘Singleton Design Pattern implementation in Java’ »

Java Generics Tutorials

Top IP Addresses – Linux shell interview question

A log file is of the following format

sessionid	ipaddress
1234		127.0.0.1
4567		127.0.0.1

Find the list of top 10 IP Addresses that have the most log entries.

Search for a class or a pattern in jar files under a folder

searchJars.sh :

-----------------------------------------------------
#!/bin/bash

while getopts "n:il" optionName; do
case "$optionName" in
i) case_sensitive_op=-i;;
l) long_format=Y;;
n) search_expr="$OPTARG";;
[?]) echo "Usage: searchJars [-i] [-l] -n  ";exit 1;;
esac
done

if [ -z $search_expr ]
then
  echo "Usage: searchJars [-i] [-l] -n ";
  exit 1;
fi

if [ $# -lt 1 ]
then
  echo "Usage: searchJars [-i] [-l] -n ";
  exit 1;
fi

REG_EXPR="$search_expr"
for i in `find . -name "*jar"`
do
  jar tvf $i 2> /dev/null | grep $case_sensitive_op $REG_EXPR > /dev/null
  if [ $? == 0 ]
  then
	echo $i
	if [ "$long_format"  = "Y" ]
	then
	   jar tvf $i | grep $case_sensitive_op $REG_EXPR | awk '{ print "		"$8}'
       echo "-----------------------------------------------------"
	fi
  fi
done
-----------------------------------------------------

Usage:
Usage: searchJars [-i] [-l] -n CLASS_NAME_OR_EXPRESSION

Awk Online Books and Tutorials

Linux Shell Scripting Online Guides and Books