Write a program to say whether a string is a rotation of another
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1. example if S1=ABCD then BCDA,CDAB etc are rotations of S1.
Just another WordPress weblog
Archive for the ‘Job Interviews’ Category.
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1. example if S1=ABCD then BCDA,CDAB etc are rotations of S1.
Given a Binary Search Tree, write a program to print the kth smallest element
This is a very common interview questions and comes in various forms. The most common one is of the following format
Problem 1
Given an array of 999 distinct integers ranging from 1 to 1000 including. Find which number is missing. Restrictions: loop over the array only once, can’t allocate an additional array.
Another version of the problem is where 2 numbers are missing.
Problem 2
Given an array of 998 distinct integers ranging from 1 to 1000 including. Find which 2 numbers are missing.
Restrictions: loop over the array only once, can’t allocate an additional array.
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.
Implement a hash table with efficient implementation for listing the elements.
Write an algorithm to shuffle an array of 52 integers so that each number has equal probability to be in any of the 52 positions.
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
You have an array of ints A[]. How would you find the two elements that sum to a particular value S?
Example:
let
A[] = {1,2,-40,4,20,53}
S=13.
Expected Answer:
53 and -40