Interview questions (2)

January 7th, 2010 admin No comments

1) There are integer arrays, A and B. what’s the best way to generate an array which contains all duplicated elements of A and B.
2) There are integer arrays, A and B. how to figure it out if those contain same elements.
3) There many of files in a folder and some of them contains phone numbers. how to find them.
4) Design restaurant reservation system. class design and data structures.

Categories: Algorithm Tags: ,

Spring MVC + Tiles

December 13th, 2009 admin No comments

1. create web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

2. create action-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>

<bean id="testController" class="com.bluewiseinc.erp.common.web.TestController">
<property name="methodNameResolver" ref="methodNameResolver" />
</bean>

<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>med</value>
</property>
<property name="defaultMethodName">
<value>userMain</value>
</property>
</bean>

<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="cache" value="false" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/UrlMap.properties</value>
</property>
</bean>
</property>
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-def.xml</value>
</list>
</property>
</bean>

<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
</beans>

3. create TestController
package com.bluewiseinc.erp.common.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class TestController extends MultiActionController {
public ModelAndView testMain(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("test/test_main", "testMain", null);
}
}

4. UrlMap.properties
## test module mapping
/test/index.do=testController

5. tiles-def.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
<definition name="defaultTemplate" template="/WEB-INF/view/tiles/default.jsp">
<put-attribute name="header" value="/WEB-INF/view/tiles/def_header.jsp" />
<put-attribute name="menu" value="/WEB-INF/view/tiles/def_menu.jsp" />
<put-attribute name="footer" value="/WEB-INF/view/tiles/def_footer.jsp" />
</definition>
<definition name="test_main" extends="defaultTemplate">
<put-attribute name="content" value="/WEB-INF/view/test/test_main.jsp"/>
</definition>
</tiles-definitions>

6. message.properties
# Page titles
index.title=Test Spring MVC + Tiles

7. default.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><fmt:message key="index.title"/></title>
<link rel=stylesheet href="${pageContext.request.contextPath}/css/global.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/global.js"></script>
</head>
<body>
<div id="header">
<div id="headerTitle"><tiles:insertAttribute name="header" /></div>
</div>
<div id="menu">
<tiles:insertAttribute name="menu" />
</div>
<div id="content">
<tiles:insertAttribute name="content" />
</div>
<div id="footer">
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>

Server Configuration Job Log

December 13th, 2009 admin No comments

Dell PowerEdge  SC1420

Dell CERC SATA 1.5/6ch RAID Controller

Windows 2003 / MS SQL 2005 / Accpac ERP

IMG_0506

1. Upgrade system BIOS, raid controller firmware on windows 2003

2. Attach a new hard disk and configure raid 0, virtual drive #1

3. Move all data on current D drive to new attached drive

4. Back up C partition as a image: Clonezilla

5. Reconfigure hard disk 1 & 2 as raid 1, virtual drive #0

6. Restore backed up C drive image to new reconfigured virtual drive #0

Categories: Server, System Tags: , , ,

Linux Server Monitoring Tools

November 4th, 2009 admin No comments

- Network Monitoring
tcptrack
iptraf

- CPU, Memory Monitoring
top
free

- Disk I/O Monitoring
iostat(sysstat)

Categories: Server, System Tags: , , ,

PHP Pear Image_Graph

September 1st, 2009 admin No comments

#apt-get install php-pear

#pear install Numbers_Words-0.16.1
#pear install Numbers_Roman-1.0.2
#pear install Image_Canvas-0.3.1
#pear install Image_Graph-0.7.2

(Download TrueType core fonts from http://corefonts.sourceforge.net/)
cp *.ttf /usr/share/php/Image/Canvas/Fonts

Image_Graph

<?php
require_once '../../includes/include.php';
require_once 'Image/Graph.php';
require_once 'Image/Canvas.php';
date_default_timezone_set('America/New_York');

$customer_id = $_REQUEST[customer_id];
if (!$customer_id) exit();
$customer_data = get_order_customer_report_xy($customer_id);
if (!$customer_data) exit();

$Canvas =& Image_Canvas::factory('png', array('width' => 575, 'height' => 280));

// create the graph
$Graph =& Image_Graph::factory('graph', $Canvas);
// add a TrueType font
$Font =& $Graph->addNew('font', 'DejaVuSans');
$Font->setSize(8);

$Graph->setFont($Font);

// create the plotarea layout
$Graph->add(
Image_Graph::vertical(
Image_Graph::factory('title', array('', 11)),
Image_Graph::vertical(
$Plotarea = Image_Graph::factory('plotarea'),
$Legend = Image_Graph::factory('legend'),
90
),
5
)
);

$Legend->setPlotarea($Plotarea);

// create a grid and assign it to the secondary Y axis
$GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY);
$GridY2->setFillStyle(
Image_Graph::factory(
'gradient',
array(IMAGE_GRAPH_GRAD_VERTICAL, 'white', 'lightgrey')
)
);

list($year, $month) = split('[-.-]', $customer_data[0][1]);
$start=mktime(0,0,0,$month,1,$year);
$today=mktime(0,0,0,date('m'),1,date('Y'));
$interv=12-$month+(12*(date('Y')-$year-1))+date('m')+1;

// create a line plot
$Dataset1 =& Image_Graph::factory('dataset');
$Dataset2 =& Image_Graph::factory('dataset');

$max = 0;
for ($i=0; $i<count($customer_data); $i++) {
if ($max<$customer_data[$i][0]) $max=$customer_data[$i][0];
}
for ($i=0; $i<$interv; $i++) {
$Dataset2->addPoint(date('m/y', mktime(0,0,0,$month,1,$year)), 0);
$month++;
}
for ($i=0; $i<count($customer_data); $i++) {
list($year, $month) = split('[-.-]', $customer_data[$i][1]);
$Dataset1->addPoint(date('m/y', mktime(0,0,0,$month,1,$year)), $customer_data[$i][0]);
}

$Plot1 =& $Plotarea->addNew('bar', array(&$Dataset1));
$Plot1->setLineColor('red');

$Plot2 =& $Plotarea->addNew(
'Image_Graph_Plot_Area',
$Dataset2,
IMAGE_GRAPH_AXIS_Y_SECONDARY
);

$Plot2->setLineColor('gray');
$Plot2->setFillColor('white@0.2');

$AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$labInterv = floor($interv / 13);
$AxisX->setLabelInterval($labInterv);
$AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$AxisY->setTitle('Orders', 'vertical');
$AxisYsecondary =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y_SECONDARY);

// output the Graph
$Graph->done();
db_close()
?>

Categories: Programming Tags: , , ,

Mount LVM disk

September 1st, 2009 admin No comments

#apt-get install lvm2

#pvscan
PV /dev/sdb1 VG server-roma lvm2 [136.49 GB / 0 free]
Total: 1 [136.49 GB] / in use: 1 [136.49 GB] / in no VG: 0 [0 ]

#vgscan
Reading all physical volumes. This may take a while…
Found volume group “server-roma” using metadata type lvm2

#vgchange -a y
2 logical volume(s) in volume group “server-roma” now active

#lvscan
ACTIVE ‘/dev/server-roma/root’ [130.91 GB] inherit
ACTIVE ‘/dev/server-roma/swap_1′ [5.58 GB] inherit

#mount /dev/server-roma/root /mnt

Categories: Server, System Tags: , , , ,

ubuntu server 9.04 grub menu.lst

September 1st, 2009 admin No comments
title           Ubuntu 9.04, kernel 2.6.28-15-server
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/vmlinuz-2.6.28-15-server root=UUID=18173536-b479-4e5f-80f6-a363503b55e1 ro quiet splash
initrd          /boot/initrd.img-2.6.28-15-server
quiet
title           Ubuntu 9.04, kernel 2.6.28-15-server (recovery mode)
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/vmlinuz-2.6.28-15-server root=UUID=18173536-b479-4e5f-80f6-a363503b55e1 ro  single
initrd          /boot/initrd.img-2.6.28-15-server
title           Ubuntu 9.04, kernel 2.6.28-11-server
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/vmlinuz-2.6.28-11-server root=UUID=18173536-b479-4e5f-80f6-a363503b55e1 ro quiet splash
initrd          /boot/initrd.img-2.6.28-11-server
quiet
title           Ubuntu 9.04, kernel 2.6.28-11-server (recovery mode)
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/vmlinuz-2.6.28-11-server root=UUID=18173536-b479-4e5f-80f6-a363503b55e1 ro  single
initrd          /boot/initrd.img-2.6.28-11-server
title           Ubuntu 9.04, memtest86+
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/memtest86+.bin
quiet

/boot/grub/menu.lst

title           Ubuntu 9.04, kernel 2.6.28-15-server
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/vmlinuz-2.6.28-15-server root=UUID=18173536-b479-4e5f-80f6-a363503b55e1 ro quiet splash
initrd          /boot/initrd.img-2.6.28-15-server
quiet

title           Ubuntu 9.04, kernel 2.6.28-15-server (recovery mode)
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/vmlinuz-2.6.28-15-server root=UUID=18173536-b479-4e5f-80f6-a363503b55e1 ro  single
initrd          /boot/initrd.img-2.6.28-15-server

title           Ubuntu 9.04, memtest86+
uuid            18173536-b479-4e5f-80f6-a363503b55e1
kernel          /boot/memtest86+.bin
quiet

Categories: Server, System Tags: , ,

Calculating distances with geographic data

August 17th, 2009 admin No comments

The great circle distance formula:
d = acos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y2-y1)) * r

CREATE TABLE ZCTA (
zcta CHAR(6) NOT NULL
, lat_degrees DECIMAL(9,6) NOT NULL
, long_degrees DECIMAL(9,6) NOT NULL
, PRIMARY KEY(zcta));

ALTER TABLE ZCTA
ADD COLUMN lat_radians DECIMAL(9,6) NOT NULL
, ADD COLUMN long_radians DECIMAL(9,6) NOT NULL;

UPDATE ZCTA
SET lat_radians = lat_degrees * (PI() / 180)
, long_radians = long_degrees * (PI() / 180);

/* calculating the distance between two points */
SELECT
ROUND(ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956, 2) AS “Distance”
FROM ZCTA x1, ZCTA x2
WHERE x1.zcta = ‘1001′
AND x2.zcta = ‘21236′;

/* zip codes within a given radius */
SELECT
x2.zcta AS zip
, ROUND(ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956, 2) AS “Distance”
FROM ZCTA x1, ZCTA x2
WHERE x1.zcta = ‘21236′
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956 <= 5
ORDER BY Distance;

/* StoreLocation table definition */
CREATE TABLE StoreLocation (
store_id INT NOT NULL AUTO_INCREMENT
, address VARCHAR(100) NOT NULL
, city VARCHAR(35) NOT NULL
, state CHAR(2) NOT NULL
, zip VARCHAR(6) NOT NULL
, PRIMARY KEY (store_id)
, KEY (zip));

/* zip codes within a given radius  with StoreLocation table*/
SELECT
address
, city
, state
, zip
FROM StoreLocation
WHERE zip IN (
SELECT x2.zcta
FROM ZCTA x1, ZCTA x2
WHERE x1.zcta = ‘21236′
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956 <= 5
);

SELECT
address
, city
, state
, zip
FROM StoreLocation s1
INNER JOIN (
SELECT x2.zcta
FROM ZCTA x1, ZCTA x2
WHERE x1.zcta = ‘21236′
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956 <= 5
) AS zips
ON s1.zip = zips.zcta;

SELECT
address
, city
, state
, zip
FROM ZCTA x1, ZCTA x2
INNER JOIN StoreLocation s1
ON x2.zcta = s1.zip
WHERE x1.zcta = ‘21236′
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956 <= 5;

SELECT
address
, city
, state
, zip
, ROUND(ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956, 2) AS “Distance”
FROM ZCTA x1, ZCTA x2
INNER JOIN StoreLocation s1
ON x2.zcta = s1.zip
WHERE x1.zcta = ‘21236′
AND ACOS(SIN(x1.lat_radians) * SIN(x2.lat_radians)
+ COS(x1.lat_radians) * COS(x2.lat_radians)
* COS(x2.long_radians – x1.long_radians)) * 3956 <= 5
ORDER BY Distance;

Incremental backup scripts

May 9th, 2008 admin No comments
#!/bin/bash
#
# creates backups of essential files
#
DATA="/home /root /usr/local/httpd"
CONFIG="/etc /var/lib /var/named"
LIST="/tmp/backlist_$$.txt"
#
mount /mnt/backup
set $(date)
#
if test "$1" = "Sun" ; then
        # weekly a full backup of all data and config. settings:
        tar cfz "/mnt/backup/data/data_full_$6-$2-$3.tgz" $DATA
        rm -f /mnt/backup/data/data_diff*
        #
        tar cfz "/mnt/backup/config/config_full_$6-$2-$3.tgz" $CONFIG
        rm -f /mnt/backup/config/config_diff*
else
        # incremental backup:
        find $DATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
        tar cfzT "/mnt/backup/data/data_diff_$6-$2-$3.tgz" "$LIST"
        rm -f "$LIST"
        #
        find $CONFIG -depth -type f  \( -ctime -1 -o -mtime -1 \) -print > $LIST
        tar cfzT "/mnt/backup/config/config_diff_$6-$2-$3.tgz" "$LIST"
        rm -f "$LIST"
fi
#
# create sql dump of databases:
mysqldump -u root --password=mypass --opt mydb > "/mnt/backup/database/mydb_$6-$2-$3.sql"
gzip "/mnt/backup/database/mydb_$6-$2-$3.sql"
#
umount /mnt/backup

>> Backup script for Linux using tar and find

Categories: System Tags: , ,

How to attach a new hard disk to ubuntu linux

May 8th, 2008 admin No comments

root@server-tribeca:/etc# fdisk -l

Disk /dev/sda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×9dc96e9e

Device Boot Start End Blocks Id System
/dev/sda1 * 1 4689 37664361 83 Linux
/dev/sda2 4690 4865 1413720 5 Extended
/dev/sda5 4690 4865 1413688+ 82 Linux swap / Solaris

Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×00a080f4

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 13 104391 83 Linux
/dev/sdb2 14 14471 116133885 83 Linux
/dev/sdb3 14472 14593 979965 82 Linux swap / Solaris

root@server-tribeca:/etc# fdisk /dev/sdb

The number of cylinders for this disk is set to 14593.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Partition number (1-4): 2

Command (m for help): d
Selected partition 3

Command (m for help): d
No partition is defined yet!

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-14593, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-14593, default 14593):
Using default value 14593

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

root@server-tribeca:/etc# mkfs.ext3 /dev/sdb1
mke2fs 1.40.8 (13-Mar-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
7331840 inodes, 29304560 blocks
1465228 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
895 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

root@server-tribeca:/etc# ls /dev/disk/by-uuid -alh
lrwxrwxrwx 1 root root 10 2008-05-07 17:58 753a86d9-6228-4725-a9ae-6f457c2b1824 -> ../../sdb1
lrwxrwxrwx 1 root root 10 2008-05-07 17:58 dbd44197-c1c5-427d-9a34-40f652b92f42 -> ../../sda1
lrwxrwxrwx 1 root root 10 2008-05-07 17:58 e15f5c2c-fc1f-414d-8c67-99c567f44d6a -> ../../sda5

root@server-tribeca:/etc# mkdir /disk1

root@server-tribeca:/etc# vi /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
# /dev/sda1
UUID=dbd44197-c1c5-427d-9a34-40f652b92f42 / ext3 relatime,errors=remount-ro 0 1
# /dev/sda5
UUID=e15f5c2c-fc1f-414d-8c67-99c567f44d6a none swap sw 0 0
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
UUID=753a86d9-6228-4725-a9ae-6f457c2b1824 /disk1 ext3 defaults,errors=remount-ro 0 1

Categories: System Tags: , , ,