1/28/2017

[Interview type questions] First pair non matching leaves

Input: Given two (binary) trees, return the first pair of non-matching leaves Tree 1: A, B, C, D, E, null, null Tree 2: A, D, B
Output: (E,B)

Example:
Tree1 :              
      A
     /   \
   B     C
  /  \
D    E    

Tree2 :
    A
  /    \
D     B

Solution:
 static class TreeNode {
  public TreeNode(char v) { val = v;}
  char val;
  TreeNode left;
  TreeNode right;
 }
 
 static class MyIterator {
  Stack<TreeNode> stack = new Stack<>();
  MyIterator(TreeNode root) {
   pullAll(root);
  }
  
  void pullAll(TreeNode node) {
   while(node != null) {
    stack.push(node);
    node = node.left;
   }
  }
  
  public TreeNode next() {
   TreeNode next = stack.pop();
   if (next.right != null) {
    pullAll(next.right);
   }
   return next;
  }
  
  public TreeNode nextLeaf() {
   while(hasNext()) {
    TreeNode next = next();
    if (next.left == null && next.right == null) {
     return next;
    }
   }
   return null;
  }
  
  boolean hasNext() {
   return stack.isEmpty() == false;
  }
 }
 
 public static void main(String[] args) {
  TreeNode root1 = new TreeNode('A');
  root1.left = new TreeNode('B');
  root1.right = new TreeNode('C');
  root1.left.left = new TreeNode('D');
  root1.left.right = new TreeNode('E');
  TreeNode root2 = new TreeNode('A');
  root2.left = new TreeNode('D');
  root2.right = new TreeNode('B');
  char[] res = findFirstNonMatch(root1, root2);
  System.out.println(res);
 }
 
 static char[] findFirstNonMatch(TreeNode root1, TreeNode root2) {
  MyIterator iter1 = new MyIterator(root1);
  MyIterator iter2 = new MyIterator(root2);
  while(true) {
   TreeNode leaf1 = iter1.nextLeaf();
   TreeNode leaf2 = iter2.nextLeaf();
   if (leaf1 == null || leaf2 == null) break;
   if (leaf1.val != leaf2.val) {
    return new char[] {leaf1.val, leaf2.val};
   }
  }
  return new char[] {};
 }
       

[Interview type questions] Construct Binary search tree from given preorder array

Input: Give a preorder array
Output: Tree root

Example: If the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be root of following tree.
     10
   /   \
  5     40
 /  \      \
1    7      50

Solution:
 static class TreeNode {
  public TreeNode(int v) { val = v;}
  int val;
  TreeNode left;
  TreeNode right;
 }

 public static void main(String[] args) {
  //       10
  //    5     40
  //  1  7       50
  int pre[] = new int[] { 10, 5, 1, 7, 40, 50 };
  int size = pre.length;
  TreeNode root = constructTree(pre, 0, size - 1);
  printInorder(root);
 }

 public static TreeNode constructTree(int[] pre, int l, int h) {
  if (l > h) return null;
  if (l == h) return new TreeNode(pre[l]);
  TreeNode root = new TreeNode(pre[l]);
  int ind = l + 1;
  // Find the first index which is greater than or equal to root
  for (int i = l + 1; i <= h; i++) {
   if (root.val < pre[i]) {
    ind = i;
    break;
   }
  }
  root.left = constructTree(pre, l + 1, ind - 1);
  root.right = constructTree(pre, ind, h);
  return root;
 }

 public static TreeNode constructTreeIterative(int[] pre, int l, int h) {
  if (l > h) return null;
  Stack<TreeNode> stack = new Stack<>();
  TreeNode root = new TreeNode(pre[l]);
  stack.push(root);
  for (int i = l + 1 ; i <= h ; i++) {
   int val = pre[i];
   // Handle left side
   if (stack.peek().val > val) {
    TreeNode left = new TreeNode(val);
    stack.peek().left = left;
    stack.push(left);
   } else {
    // Handle right side and find its paraent
    TreeNode rightParent = null;
    while(!stack.isEmpty() && stack.peek().val < val) {
     rightParent = stack.pop();
    }
    rightParent.right = new TreeNode(val);
    stack.push(rightParent);
   }
  }
  return root;
 }

 public static void printInorder(TreeNode node) {
  if (node == null) {
   return;
  }
  printInorder(node.left);
  System.out.print(node.val + " ");
  printInorder(node.right);
 }
       

1/27/2017

[Interview type questions] Divide friends into two groups, and people in the same group do not know each other

Input: Give edges to represent their relations.
Output: Divide the friends into two groups, and people in the same group do now know each other.

Example: Give 4 persons. [0, 1] [1, 2] [1, 3]
         0 and 1 know each other, 1 and 2 know each other...
         We can take 0,2 as group 1 and 1,3 as group 2.

Solution: Use bipartite graph with BFS to resolve this problem:
  int n = 4;
  int[][] relations = new int[][] { { 0, 1 }, { 1, 2 }, { 2, 3 } };

  // Build a graph and initialize groups
  HashMap<Integer, Set<Integer>> graph = new HashMap<>();
  int[] group = new int[4];
  Arrays.fill(group, -1);
  for (int[] edge : relations) {
   Set neighbors = graph.getOrDefault(edge[0], new HashSet<>());
   neighbors.add(edge[1]);
   graph.put(edge[0], neighbors);
  }
  // BFS, start with 0
  Queue queue = new LinkedList<>();
  queue.offer(0);
  int groupdId = 0;
  group[0] = groupdId;
  while(queue.isEmpty() == false) {
   int v = queue.poll();
   groupdId = 1 - group[v];
   // Get its neighbors
   // No neighbors..
   if (graph.get(v) == null) continue;
   for (int neighbor : graph.get(v)) {
    if (group[neighbor] == -1) {
     group[neighbor] = groupdId;
     queue.offer(neighbor);
    } else if (group[neighbor] == group[v]) {
     // It cannot be divided into two group. Invalid bipartite graph
     break;
    }
   }
  }
  
  for (int g : group) {
   System.out.println(g);
  }
       

1/10/2017

Serialize/De-serialize data to a file using Flatbuffers


Read & Write

       

#include "flatbuffers/util.h"

flatbuffers::FlatBufferBuilder builder;

// Create something using builder
// ......

// Save to file
bool result = flatbuffers::SaveFile(filename.c_str(),
                                      (const char *) builder.GetBufferPointer(),
                                      (size_t) builder.GetSize(), true);

// Load from file
std::string buffer;
result = flatbuffers::LoadFile(fileName, true, &buffer);
printf("\nLoadFile Result = %d", result);


// You can write your own Save/load function
bool MySaveFile(const char *name, const char *buf, size_t len,
                     bool binary) {
  std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
  if (!ofs.is_open()) return false;
  ofs.write(buf, len);
  return !ofs.bad();
}

bool MyLoadFileRaw(const char *name, bool binary, std::string *buf) {
  std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
  if (!ifs.is_open()) {
    return false;
  }
  if (binary) {
    // The fastest way to read a file into a string.
    ifs.seekg(0, std::ios::end);
    auto size = ifs.tellg();
    (*buf).resize(static_cast(size));
    ifs.seekg(0, std::ios::beg);
    ifs.read(&(*buf)[0], (*buf).size());
  } else {
    // This is slower, but works correctly on all platforms for text files.
    std::ostringstream oss;
    oss << ifs.rdbuf();
    *buf = oss.str();
  }
  return !ifs.bad();
}
       
 


12/25/2016

Install the latest cmake to Ubuntu

Step 1 : Download the latest and stable cmake from https://cmake.org/download/

I download the source of cmake with v3.7.1 to my Ubuntu 14.04.

Step2 : Extract the compressed file and build it

$ cd cmake-3.7.1
$ ./bootstrap --prefix=/usr
$ make
$ sudo make install

Step3 : Check it

$ cmake --version

Expected output:  cmake version 3.7.1

12/07/2016

Make python program executable and compile for Windows exe on Linux

  This week, I spend some time to make one of my repositories, LablelImg,  on Github become an executable file. The LablelImg is implemented  in Python, and it has some dependencies like lxml and pyQt. In order to let other people use the tool easier without installing python and their dependencies, I decide to make it a standalone executable. I described as below about how I have done to compile it for Window exe and Linux binary on Ubuntu. In Python, there are some packages which can let python files become an executable file, e.g pyexe, pyinstaller, and so on.
Then, I decide to pick pyinstaller because its document and relevant reference are concise.

Environment : 

OS: Ubuntu 14.04
Necessary packages and tools :
  1.   wine (1.6.2)
  2.   pyinstaller (2.1)
  3.   virtual-wine (0.1)
  4.   python-2.7.8.msi
  5.   pywin32-218.win32-py2.7.exe 
  6.   PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe
  7.   lxml-2.3.win32-py2.7.exe
The steps to install or download them:
wine : $ apt-get install wine
pyinstaller : $ git clone https://github.com/pyinstaller/pyinstaller
virtual-wine : $ git clone https://github.com/htgoebel/virtual-wine.git
python-2.7.8 : Download from https://www.python.org/ftp/python/2.7.8/python-2.7.8.msi
pywin32-218.win32-py2.7 : Download from http://nchc.dl.sourceforge.net/project/pywin32/pywin32/Build%20218/pywin32-218.win32-py2.7.exe
PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe: https://www.riverbankcomputing.com/software/pyqt/download
lxml-2.3.win32-py2.7.exe : Download from  https://pypi.python.org/pypi/lxml/2.3/

Install packages to virtual env

Create a virtual wine environments using virtual-wine:
$ apt-get install scons
$ ./virtual-wine/vwine-setup venv_wine

Active virutal env:
$ . venv_wine/bin/activate

Use wine to install python packages to your virtual env: 
$ wine msiexec -i python-2.7.8.msi
$ wine pywin32-218.win32-py2.7.exe
$ wine PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe
$ wine lxml-2.3.win32-py2.7.exe

Build your python files using pyinstall under virtual environment

$ wine c:/Python27/python.exe pyinstaller/pyinstaller.py -D -F -n AppName -c "main.py" 
The exe file will be located under dist directory

Check out my scripts, you will be more clear

Check out my script to setup the environment and make pyQt executable.
https://github.com/tzutalin/labelImg/tree/master/build-tools

11/27/2016

Record Android touch event or other events

  Recently, I met a problem which I cannot use Android-unit-test to do some of the test cases. I browse through some of apps or tools, but they are not handy for me. So I wrote a simple python tool to record my Android events like touching, gyro, and so on. Then, I can use my tools on my PC to playback the events I recorded. It's very convenient for me to reproduce some issues.

https://github.com/tzutalin/adb-event-record


11/02/2016

[Ubuntu Network] Share internet connection from mobile phone to PC via USB


Mobile

Go to Setting -> Network share -> Turn on USB network share

PC

Configuring the PC that will connect to the network which is provided by Mobile phone. Open your Network Manager via the Network Icon on the Unity Panel.


10/26/2016

Setup ssh connection on Ubuntu

Install necessary packages:



Host:
sudo apt-get update
sudo apt-get install openssh-server
sudo ufw allow 22
Client:
sudo apt-get install openssh-client


Connect usage:

ssh username@host

Advanced:

On the client side, create the host alias for ssh. Edit ~/.ssh/config, then copy the below pattern to the config file
Host workstation
HostName 10.70.70.44
User darrenl
Port 22
Then, we can simply the usage to connect to host
ssh workstation
Copy the public key to remote host using ssh-copy-id, thus, we didn't need to type password every time

ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host


-