..MindWrite..

Posts Tagged ‘interview’

Why char array is preferred over String for storing password?

Posted by guptaradhesh on August 23, 2013

String is immutable in java and stored in String pool. Once it’s created it stays in the pool until unless garbage collected, so even though we are done with password it’s available in memory for longer duration and there is no way to avoid it. It’s a security risk because anyone having access to memory dump can find the password as clear text.

If we use char array to store password, we can set it to blank once we are done with it. So we can control for how long it’s available in memory that avoids the security threat with String.

Advertisements

Posted in java, tech | Tagged: , , , | Leave a Comment »

Print matrix in a sprial order

Posted by guptaradhesh on August 22, 2013

 

Recursive Solution (Java):

public class PrintMatrixSpiral
{
 public static void main(String[] args)
 {
  int[][] matrix =
  {
  { 2, 4, 6, 2, 5, 7 },
  { 2, 5, 7, 8, 9, 3 },
  { 6, 4, 7, 3, 5, 7 } };

  printSpiral(matrix);
 }

 public static void printSpiral(int[][] matrix)
 {
  printSpiral(matrix, 0);
 }

 private static void printSpiral(int[][] matrix, int depth)
 {
  if (matrix == null && matrix.length == 0)
   return;
  int rows = matrix.length;
  int cols = matrix[0].length;
  if (2 * depth > Math.min(rows, cols))
   return;
  for (int i = depth; i < cols - depth - 1; ++i)
  {
   System.out.print(matrix[depth][i] + ",");
  }
  for (int i = depth; i < rows - depth - 1; ++i)
  {
   System.out.print(matrix[i][cols - depth - 1] + ",");
  }
  for (int i = rows - depth; i > depth; --i)
  {
   System.out.print(matrix[rows - depth - 1][i] + ",");
  }
  for (int i = rows - depth - 1; i > depth; --i)
  {
   System.out.print(matrix[i][depth] + ",");
  }
  printSpiral(matrix, ++depth);
 }
}


Posted in java, puzzles/ algorithms, tech | Tagged: , , , , | Leave a Comment »

Some Interview puzzles by Apple

Posted by guptaradhesh on April 18, 2013


http://www.businessinsider.com/apple-brain-busting-interview-questions-answers-2012-6?op=1

Posted in puzzles/ algorithms, tech | Tagged: , , , | Leave a Comment »