Showing posts with label o. Show all posts
Showing posts with label o. Show all posts

Friday, February 17, 2017

2. [Technical Interview] [Java] - Reverse String in Place

Key Points
- Only traverse the string halfway and that is all that is needed to reverse the string in place.
- Use StringBuilder to reverse the string in place, not the same as making another copy of string.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.lang.Math; // headers MUST be above the first class
import java.util.*;
import java.lang.*;
import java.io.*;

// one class needs to have a main() method
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println(isPalindrome("appa"));
    System.out.println(reverse("hellop"));
  }
  
  private static boolean isPalindrome(String s)
  {
    int n = s.length();
    
    for (int i = 0; i < (n/2); i++)
    {
      if (s.charAt(i) != s.charAt(n - i - 1))
      {
        return false;
      }
    }
    
    return true;
  }
  
  private static String reverse(final String input)
  {
    final StringBuilder builder = new StringBuilder(input);
    int length = builder.length();
    
    for (int i = 0; i < length/2; i++)
    {
      final char current = builder.charAt(i);
      final int otherEnd = length - i - 1;
      
      builder.setCharAt(i, builder.charAt(otherEnd));
      builder.setCharAt(otherEnd, current);
    }
    
    return builder.toString();
  }
}

1. [Technical Interview] [Java] - Find Palindrome

Key Points
- Key is to stop checking halfway through the string to be efficient.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.lang.Math; // headers MUST be above the first class
import java.util.*;
import java.lang.*;
import java.io.*;

// one class needs to have a main() method
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.print(isPalindrome("appa"));
  }
  
  private static boolean isPalindrome(String s)
  {
    int n = s.length();
    
    for (int i = 0; i < (n/2); i++)
    {
      if (s.charAt(i) != s.charAt(n - i - 1))
      {
        return false;
      }
    }
    
    return true;
  }
}

11. [Tutorial] How to Implement Android App with Node.js Server and MongoDB

Information - I followed this tutorial:  https://www.learn2crack.com/2014/04/android-login-registration-nodejs-server.html - But that link...