So unless you like Netbeans, it might be a series of manual steps to create a Spring WebMVC application setup. A good idea is to use the SpringFramework tutorial as given on http://static.springframework.org/docs/Spring-MVC-step-by-step/index.html
There is a small error in the tutorial, so to save you time if you use this, watchout for the extra “<beans>” tag. So your springapp-servlet.xml in Chapter 4 should look like
<?xml version=”1.0″ encoding=”UTF-8″?>
Rownum is a special column that can be used to limit the number of results returned by a query. However, not understanding exactly how it works can lead to unexpected results. Let’s go into some simple scenarios involving the rownum clause.
Here’s our table definition:
create table employee (empname varchar(20), empid number(10));
Let’s say we want to retrieve 5 rows from this table. This is what the query might look like.
select * from employee where rownum <= 5;
Now if we want the top 5 empids from this table, here’s a novice attempt. This query will not return the top 5 empids as desired, but just some 5 rows sorted by empids.
select * from employee where rownum < 5 order by empid desc;
Here’s what we need to do. What follows is called a Top-N query. This query contains a nested query that retrieves the rows from this table sorted by the empids, and then applies a rownum clause on the results of this query to return the desired result.
select * from (select * from employee order by empid desc) where rownum <=5;
Essentially a Top-N query is a query used to retrieve the top N rows from a table, based on desired conditions. In our example, our query was attempting to retrieve the top 5 empids from the employee table.
(Please note that all queries listed here were run and tested on Oracle.)
Problem: Compute all prime numbers less than specified integer
Output: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
We use an algorithm known as the Sieve of Eratosthenes to solve this problem. How it works is that for each prime number starting from 2, we mark all multiples of that number as non prime. At the end of this process, all the prime numbers are identified.
Here’s the detailed steps.
1. Create an array of n items, and mark all values as 1 to mark them as prime.
2. Iterate over the array going from i = 2 to sqrt(n). If i is prime, mark all multiples of i as 0 (non prime)
Problem: Given two input strings, remove all occurrences of all the letters in the second string from the first one
Input: “Battle of The Vowels”, “aeiou”, Output: “Bttl f Th Vwls” (removing all vowels from the input string)
We could iterate over the remove string and for each character, remove all occurrences from the input string. Give input string of length ‘n’ and remove string of length ‘m’, this brute force algorithm would lead to a complexity of O(mn).
Here’s a more efficient algorithm. Iterate over the remove string, and store each character in an array. Later as we iterate over the input string, we lookup this array, and if that character is present in this array, then we remove it from the input. This algorithm works if the array lookup is a constant time operation. The net complexity of this algorithm would be O(m+n), much better than the O(mn) brute force algorithm described earlier.
The detailed steps are as folllows:
1. Create an array ‘flags‘ of 256 characters. Entries in this array serve as flags denoting whether character ‘i’ is to be removed or not. Initialize all elements of this array to false.
2. Iterate over the remove string. For each character i in the string, mark flags[i] as true
3. Iterate over the input string. For each character in the string, if flags[i] is true, then copy it to an output array. Otherwise continue to the next character.
4. At the end of this iteration, the output array will contain all characters of input minus all letters of remove.
Problem: To find the nth position element in an Inorder traversal of a Binary tree. Each node in the Binary tree contains the size of the tree underneath.
For a tree with root:node1, left child: node2, right child: node3
Input: 1, Output: node2
Input: 3, Output: node3
1.Traverse the tree to find the node where required inorder position is equal to weight of a node
2.Weight of each node is determined by keeping track of the size of the tree traversed.
3.If node’s weight is less than inorder position then go left else go right and carry the weight of the current node
Posted in Java, Programming Practice, Software, Technology
Tagged Binary tree, code sample, Find nth position element in an Inorder traversal, Inorder traversal, Java, Programming Practice, reverse a sentence, technical interview
Problem: Detect whether a given string is a palindrome or not. A palindrome is a word that spells the same if it is reversed.
Input: “madam”, Output: true
Input: “test”, Output: false
Input: “a”, Output: true
Input: “aabbaa”, Output: true
1. Initialize two counters, one pointing to the first character, and the second to the last one
2. Iterate over the string. Compare the characters at the two locations. If the characters match then advance the front counter by 1 and the back counter by -1. If the characters don’t match, the string is not a palindrome.
3. If the two counters reach the same location, and everything has matched so far, the string is a palindrome
Problem: Given a string of words, reverse the string such that the last word becomes the first one. The word itself should not be changed
Input: “Hello world”, Output: ” World Hello”
Input: “This blog rocks”, Output: ” rocks blog this”
1. Initialize two counters, front and back, to the end of the input string
2. While we havent reached the start of the string, move down front we encounter a space. Copy the part of the string from this point until back into the output.
3. Move back down by one character to account for the space. Set front to back, and repeat till front is more than 0.