Posts

Maximum Subarray

Problem Statement : How to find the maximum sum of a sub array. Example : for the array [1,2,-3,1] maximum sum possible is for the sub array [1,2] = 3.                   for the array [10,5,-24,20,-1,35] maximum sum possible is for the sub array [20,-1,35]=54 Solution :  Kadane's Algorithm provides 0(n) solution to solve the problem.  Kadane's algorithm keeps track of the maximum value of a sequence ending at the current point, and the maximum value seen so far. Following Code sample demonstrates a method which can be used to find the maximum sub array. public int getMaximumSubarray(int inputArray[]) { int max_ending_here = 0, max_so_far = 0; for (int i = 0; i < inputArray.length; i++) { max_ending_here = max(0, max_ending_here + inputArray[i]); max_so_far = max(max_so_far, max_ending_here); } return max_so_far; }

Masking Credit Card number in Java

Sometimes we need to mask crucial information like Credit Card Numbers, CVV numbers etc before storing  or logging the information. This example mask Credit Card Number (Except last 4 Digit) from a Text which contains information along with Credit Card Number. The following example demonstrates how we can easily mask the credit card with Matcher and Pattern Classes. This Sample Code uses Matcher and Pattern. Pattern Used in this sample is not optimized for Credit Card Numbers, this pattern will get any numerical numbers in the String Content.  Based on the Credit Card Type a more efficient and Strict RegEx can be used to mask the Credit Card. /**Mask the Credit card number but last four digit value **/   Pattern PATTERN = Pattern.compile( "[0-9]+" ); String message = content; Matcher matcher = PATTERN.matcher(message); String maskingChar = "*"; StringBuilder finalMask = new StringBuilder(maskingChar...

Windows Developer Help: Knowing a Process with port and killing it

Image
Sometimes we need to know the application or process that is using a particular port and kill or stop that process in a Microsoft Windows based PC. Following command (Netstat ), shows list of process and port it is using Netstat -a -n -o You can pipe the response with more command like follows, Netstat -a -n -o | more Once we know the Process ID(PID) of the process using a particular port, we can use the following command to know more about the process or kill it. tasklist /svc  /FI "PID eq 1196"    ( Get details about the process) taskkill /PID 827                          (Killl the process, similar to ps -ef in linux)

Shift operator in java

Shift operator in java Java supports two types of shift operator (Logical Shift and Arithmetic shift). Logical Shift : In logical shift simply all the bits except the extreme left or right bit is moved by one place. and The extra bit from the other end is replaced with zero. For Example for The number 4 (binary = 0000 0100) Logical Shift ( >>>, << )                              original number        0 000 0100              (Decimal 4)                              after left shift           000 0100 0              (Decimal 8)                              original number         ...

Formating XML for Pretty Printing

The following Class is an example which we can use to Pretty Printing XML. method parseXmlString() reads a String and build a Document Object. method formatXMLString() uses Transformer Object's Outputproperties to set features like Encoding, Indentation etc. public class XmlFormatter { public String formatXMLString(String string) throws Exception { final Document document = parseXmlString(string); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "no"); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); } private Document parseXmlString(String in) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstan...

Choosing Java Collection

The following section summarizes the various Collection Classes as of Java 7 and their important attributes Including HashMap which is not in Collection Framework. HashMap. It provides constant time operation for operations like get and put and remove. Iteration over the Collection values requires time proportional to the capacity of the Map.(values()). Default load factor is .75. HashMap does not maintain the order. HashMap is not synchronized. LinkedHashMap LinkedHashMap orders are maintained It provdes  constant-time performance for operations like get, put and remove. Performace is slightly slower then HashMap due to the overhead of maintaining Linked List. Re-insertion does not effect the insertion order. Iteration over the collection values is proportional to the size of the map    regardless capacity . TreeMap Red-Black tree based implementation. Implement the SortedMap Interface. Map is ascending Key order of the natural order. It has log(n) ...

Difference between wait() and sleep methods in Java

The following section captures the main difference between wait and sleep method wait method wait sends the current thread to non-runnable  state thread releases the lock on the object before going to non-runnable state. wait ()  method is defined on Object class wait ()  is a instance method wait() should be called when the thread has already  acquired  the lock on the object the thread will be in non-runnable state untill the time is elapsed or another thread  invoked  notify() / notifyAll() method on the object sleep ()  method sleep ()  method will send the current thread to non-runnable state for the specified time. sleep() method is defined in Thread class sleep() method is a Static method. sleep ()  does not cause the thread to releases the lock on the object