3/22/2012

Brute Force SAT Solver with Scheme


Produced some code solving the SAT problem with scheme, a functional language. It's not perfect and might caused the application to crash when the number of literals of an assignment is more than 25.. One problem is the allassign function: it will save much more space with tail recursion.. But I was too lazy to change my code.

#lang racket

;counts the the number of clauses
(define (countclauses data)
  (cond
    ((null? data) 0)
    ((= (car data) 0) (+ 1 (countclauses (cdr data))))
    (#t (countclauses (cdr data)))))

;determines the maximum variable number
(define (maxvar data)
  (cond
    ((null? data) 0)
    (#t (let* ((max (maxvar (cdr data)))
               (c (car data))
               (a (if (< c 0) (- c) c)))
          (if (> a max) a max)))))

;takes a single argument, a list of integers representing a SAT problem in DIMACS format,
;and returns a list of lists, where each list represents a clause.
(define (getClauses lis)
  (saveClause lis '()))

(define (saveClause lis temp)
  (cond 
    ((null? lis) '())
    ((zero? (car lis)) (append (list temp) (saveClause (cdr lis) '())))
    (else (saveClause (cdr lis) (cons (car lis) temp)))))

;takes a positive integer n and generates all possible assignments of length n.
(define (allassign num)
  (allassign2 num '(())))

(define (allassign2 num temp)
  (cond
    ((zero? num) temp)
    (else (append (allassign2 (- num 1) (list (cons #t (car temp)))) (allassign2 (- num 1) (list (cons #f (car temp))))))))

;takes a variable and a list representing an assignment
;returns the value of that variable under that assignment
(define (eval-var var lis)
  (cond 
    ((eq? var 1) (car lis))
    (else (eval-var (- var 1) (cdr lis)))))

;similar function to evaluate a literal
(define (eval-lit var lis assgn)
  (cond
    ((eq? var 1) (if (< (car lis) 0) (not (eval-var (opposite (car lis)) assgn)) (eval-var (car lis) assgn)))
    (else (eval-lit (- var 1) (cdr lis) assgn))))

(define (opposite var)
  (if (< var 0) (- 0 var) var))

;evaluate a clause
(define (eval-clause var lis assgn)
  (cond
    ((eq? var 1) (eval-clause2 (car lis) assgn))
    (else (eval-clause (- var 1) (cdr lis) assgn))))

(define (eval-clause2 lis assgn)
  (cond
    ((null? lis) #f)
    ((eq? #t (eval-lit 1 lis assgn)) #t)
    (else (eval-clause2 (cdr lis) assgn))))

;evaluate a CNF
(define (eval-form lis assgn)
  (cond
    ((null? lis) #t)
    ((eq? #f (eval-clause 1 lis assgn)) #f)
    (else (eval-form (cdr lis) assgn))))

;main function, determines whether a list of integers is satisfiable
(define (sat lis)
  (SATsolver2 (getClauses lis) (allassign (maxvar lis))))

(define (SATsolver2 lis assgn)
  (cond
   ((null? assgn) #f)
   ((eq? #t (eval-form lis (car assgn))) #t)
   (else (SATsolver2 lis (cdr assgn)))))

3/10/2012

SAT Solver

In computer science, satisfiability (often written in all capitals or abbreviated SAT) is the problem of determining if the variables of a given Boolean formula can be assigned in such a way as to make the formula evaluate to TRUE.

SAT was the first known example of an NP-complete problem. That briefly means that there is no known algorithm that efficiently solves all instances of SAT, and it is generally believed (but not proven, see P versus NP problem) that no such algorithm can exist.

3-satisfiability is a special case of k-satisfiability (k-SAT) or simply satisfiability (SAT), when each clause contains exactly k = 3 literals.


3-SAT is NP-complete and it is used as a starting point for proving that other problems are also NP-hard. This is done by polynomial-time reduction from 3-SAT to the other problem.

EG. Subset sum problem
Assume that we are given some integers, such as {-7, -3, -2, 5, 8}, and we wish to know whether some of the integers can add up to zero. As the number of integers that we feed into the algorithm becomes larger, the number of subsets grows exponentially, and in fact the subset sum problem is NP-complete. An algorithm that verifies whether a given subset has sum zero is called verifier. A problem is said to be in NP if and only if there exists a verifier for the problem that executes in polynomial time.

3/06/2012

Assessment with Epic

It took me 3.5h today to finish the test, from 11:00am to 2:30pm. There are three parts of the test: Math, Understanding a new language, and coding. Epic gives 5h maximum but time you use is also an evaluation.

1. Math problem. 14 of them I think. Easier than GRE quantity but has some weird questions. Mainly because I cannot understand what it is asking..
Eg. A goat jumps 3ft height per minute, and slide back 2ft per minute. If it wants to jump out of a cliff, which is 50.5ft, from the bottom. How many minutes does it take?

2. Given a new language, understand it and answer questions.
A new language is given and there are 20 questions. Not that hard at all.
Eg. the language has two forms of variables: string and integer, expressions are strictly operated left to right, boolean expressions. Variables are not declared type explicitly.
Functions like SET, READ, KILL.

3. Programming skills.
1) Long Subtraction -- Given two arrays A, B, each contains elements of digits, return an array of A - B. Your machine can only do calculation of less than 20.
eg. A = [1,2,5,7,5];
B = [3,4,8,9];
A - B = [9,0,8,6];

2) I forgot

3) Given a keyboard with every letter maps a digit from 0 to 9, return all possible permutation of given a n digit number.

eg. 0 <- z,a,q,x,s,w
1 <- c,d,e
2 <- v,f,r
3 <- b,g,t
...
Then permutation of num 1230 will be:
cvbz
cvba
cvbq
...

4) There is one kind of numbers call Fibonacci number (forgot the specific name), which satisfy the following situation:
A. can be spilt into several numbers;
B. The first two number are the same, the next number is equal to the sum of previous two
eg. 112 (2 = 1 + 1), 12,122,436(12 + 12 = 24, 12 + 24 = 36)
If you are given a range by the user, print all numbers that are Fibonacci number in the range.

I finished the first and second part with less than 1h, while Epic gives more than 2h to do. But I cannot finish question 4 on time, even though I was given more than 110 min...

3/04/2012

搭建Ruby on Rails环境

http://ruby.railstutorial.org/chapters/a-demo-app#sec:planning_the_application
0. 安装和配置git。具体上github官网看
1. 安装Ruby,网上说最好使用rvm,这样子有利于gemset的安装位置放在一起,也有利于切换不同的ruby版本。凄凉的是最好不要同时装1.8.7和1.9.3啊。。本人试了两次每次总是找不到rails,因为默认版本1.8.7,把default改成1.9.3重启又没了,麻烦死了,没那个需求还是别装那么多。
2. 安装gem,会发现用rvm装完ruby1.9.3以后自动已经配好了最新的gem
3. 安装rails,利用gem install rails直接安装最新版本的rails
4. 这时候可以测试一下,用rails new proejctname来做一个project,然后bundle install一下,跑rails server即可。
5. 纠结了很半天git要怎么做push pull fetch等等。暂时还没有明白github的版本控制到底优良在哪里。做提交的时候首先是可以git push origin master,这时提示哪些文件可以add,利用add filename,然后commit即可。
    而从repo上第一次下载到本地有个很纠结的地方:就是这个repo是不是你自己的?如果你不是admin则一般有两种协作方式让你进行版本控制。
一种是fork到自己repo里进行push commit等。另一种是在源项目里创建一个shared的branch和repo,然后push的东西用那个shared的branch提交的相应的repo里。两种都不直接commit到master上,因为这样不利于协作。

先写这么多,其实还没弄太懂,明天继续。

3/03/2012

Interviews I have been gone through

Since last semester I have been applying for summer internship of software development. Companies I was interviewed included Amazon, Google, Morgan Stanley, Bloomberg, Epic, Robustlinks (startup), Moonlyt (startup), Douban. And I think I am still waiting for more company interviews in the following weeks.

Right now I only have offers from the startups and was rejected by all the larger scale companies. And in the following part I will explain all the interview process from big firms and how I regard each firm. Also some thoughts about why I didn't get the offers.

1. Amazon
I applied Amazon in Oct 2011. I got two phone interviews and after two or three weeks I got the third one. A week later I was told rejected.

Questions in the first interview:
What's a Hashmap? How to make a good Hashmap?
Given the pre-order and in-order array of a tree. Can you represent the tree without knowing the elements inside the array?

Question in the second interview:
We have a chess grid (table) which is 8x8, we’ll index the cells from 0 to 7 both vertically and horizontally :
(0,0) is the  top left
(7,7) right bottom and our "target"

An ant is situated in the top left corner (0,0) and she needs to reach the bottom right corner (7,7).

The ant can only move forward to the target , one square at a time either horizontally or vertically.

More formally, the ant can move either:
from       (i,j)  -> (i+1, j)  if    i+1 <= 7
or from    (i,j ) -> (i, j+1)  if     j+1 <= 7
1) write a program that will display all possible paths to the standard output.
2) How many paths there are ?

Questions in the third interview:
1. Sum up all the prime number from 1 to 100.
2. Asked me about what is polymorphism, what is encapsulation.

It's way too early at that time as my English is not that fluent and I haven't prepared for anything. Thus the first interview is a mass and I totally have no idea how Amazon ask questions and what I should be answering.

2. some technical company
Applied in November. Got two phone interview in the same day of early December. Got rejected the next day.

First interview:
1. write function in Java which takes an array as a number, and return the increment of the array by one.

eg.
[2,4,7] -> [2, 4, 8]
[1, 0 , 0 , 3] -> [1 ,0 ,0,4]
[4,5,9] -> [4,6,0]
[1, 9 , 9] -> [2, 0 , 0]
[9,9] -> [1,0,0]



2. You have machine with limited amount of memory: 1024 bytes.
Write the longest running program - which will terminate at some point.
long = as much time as possible

What's the running time of your code? How can you proof your code to have the longest running time?

Second interview:
1. What's the function of the following code? And does it have any bugs?

public class Generator {
 private static final Map<byte[], byte[]> cache = new HashMap<byte[], byte[]>();

 
 public static byte[] generate(byte[] src) {

   byte[] generated = cache.get(src);
   if (generated == null) {
     synchronized (cache) {
       generated = cache.get(src);
       if (generated == null) {
         generated = doGenerate(src);
         cache.put(src, generated);
       }
    }
  }
 return generated;
}

private static byte[] doGenerate(byte[] src) {...}
}

2. Write code for the Fibonacci function.

3. Morgan Stanley
Applied through careerNet. Got oncampus interview. And was rejected several days later.
Questions are pretty general. Not much technical questions.


4. Bloomberg
1. Why bloomberg?
2. Discussed something about differences between Java and C++, then talk about JVM
3. Given a number and a base, return 转换后以那个该进制的数。
eg. number is 5, base is 3, then then return 12
4. You have three baskets with fruits inside and with tags outside. One of them has apple, one has orange and one has both. Now you know they are definitely tagged mistakenly. You can pick up as many fruits as you want. How many times do you have to pick the fruits to determine the fruits in the baskets.