您的位置:首页 > 职场人生

风林火山高级java工程师面试题

2014-08-18 00:00 393 查看
最近胆子大了点,直接投了风火山林的高级java工程师职位,对于我这种刚毕业的菜鸟来说能挺到第三轮,可能对大家来说还算可以,但我心里面对自己的要求可不是那样的。第三轮是算法题,就是发几个题目给你两天时间做完,实际上我一天就做完了,只是在算法的复杂度上没有考虑,我想这也是被刷下来的原因之一吧 。

题目如下:

这是工具类:

package www.work.service;

import java.util.List;

public interface Work_One {

public List<Integer> exec(int n,Integer[] data)throws Exception;

}

package www.work.service.impl;

import java.util.ArrayList;

import java.util.List;

import www.work.service.Work_One;

import www.work.util.CheckArray;

import www.work.util.Work_OneQuicksort;

/**题目描述:1. 请实现一个函数:凑14;输入很多个整数(1<=数值<=13),

* 任意两个数相加等于14就可以从数组中删除这两个数,求剩余数(按由小到大排列);

* 比如: 输入数组[9,1,9,7,5,13], 输出数组[7,9]

*

*
@author 陈砚庭

*

*/

public class Work_OneQSort implements Work_One{

/**

*
@param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Work_OneQuicksort q=new Work_OneQuicksort();

Work_One qSoft=new Work_OneQSort();

Integer[] data={1,2,13,2,13,1,4,6,5,7,4,7,3};

q.setData(data);

q.sort(0, data.length-1);

q.display();

List<Integer> lists=null;

try {

if(CheckArray.checkArray(data))

lists = qSoft.exec(14, data);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(lists);

}

public List<Integer> exec(int n,Integer[] data)throws Exception{

if(data==null){

throw new NullPointerException();

}

if(n<0){

throw new IllegalArgumentException();

}

Integer temp=n%2==0? n/2:n/2+1;

if(data[0]>temp||data[data.length-1]<temp){

return null;

}

for(int i=0;i<data.length;i++){

if(data[i]>temp){

data[i]=n;

}else{

for(int j=i;j<data.length;j++){

if(data[i]+data[j]==n){

data[i]=0;

data[j]=0;

}

}

}

}

return getResult(n,data);

}

/**

* 把数组中多余的元素剔除掉

*
@param n

*
@param arg0

*
@return

*/

private List<Integer> getResult(int n,Integer[] arg0){

List<Integer> lists=new ArrayList<Integer>();

for(int i=0;i<arg0.length;i++){

if(!(arg0[i]==0||arg0[i]==n)){

lists.add(arg0[i]);

}

}

return lists;

}

}

package www.work.service;

import java.util.List;

public interface Work_Two {

/**

* 根据一维坐标获取重叠部分

* @param array

* @return

* @throws Exception

*/

public List<List<Double>> getSegment(Double[][] array)throws Exception;

}

package www.work.service.impl;

import java.util.Arrays;

import java.util.LinkedList;

import java.util.List;

import www.work.service.Work_Two;

/**

* 问题描述:2. 请实现一个函数:线段重叠; 输入多个一维线段,

* 求出这些线段相交的所有区域(也用线段表示);

* 一条线段用两个值表示(x0,x1), 其中x1>x0;

*比如: 输入线段数组[(2,4),(1.5,6),(0.5,3.5),(5,7),(7.5,9)], 输出线段数组[(1.5,4),(5,6)]

* @author 陈砚庭

*

*/

public class Work_TwoSegment implements Work_Two {

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

// TODO Auto-generated method stub

Double[][] array={{2.0,4.0},{1.5,6.0},{0.5,3.5},{5.0,7.0},{7.5,9.0}};

Double[][] arrayInt={{2.0,4.0},{4.0,6.0},{6.0,7.0},{7.0,7.0},{7.5,9.0}};

Double temp[][]={{1.5,6.0},{2.4,7.8}};

Work_Two two=new Work_TwoSegment();

System.out.println(two.getSegment(array));

}

public List<List<Double>> getSegment(Double[][] array) throws Exception {

if (array == null) {

throw new NullPointerException();

}

//将二维数组转化为一维数组

Double[] temp = new Double[array.length * 2];

int[] count = new int[array.length * 2];//存放点的基数的数组,基数越大代表该点被包含的区间越多

for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

temp[i * array[i].length + j] = array[i][j];

}

}

Arrays.sort(temp);

Double x = 0.0;

Double y = 0.0;

for (int i = 0; i < array.length; i++) {

x = array[i][0];

for (int j = 1; j < array[i].length; j++) {

y = array[i][j];

for (int k = 0; k < temp.length; k++) {

if (temp[k] >= x && temp[k] < y)

++count[k];

}

}

}

List<List<Double>> resultList = new LinkedList<List<Double>>();

List<Double> list = null;

int flag = 0;

for (int i = 0; i < count.length; i++) {

if (count[i] > 1 && flag == 0) {

flag = 1;

list = new LinkedList<Double>();

list.add(temp[i]);

} else if (count[i] == 1 && flag == 1) {

flag = 0;

list.add(temp[i]);

resultList.add(list);

list = null;

}

}

return resultList;

}

}

package www.work.service;
import java.util.ArrayList;
public interface Work_Three {
public ArrayList<ArrayList<Integer>> getLongest(Integer[] array) throws Exception;
}
package www.work.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import www.work.service.Work_Three;
import www.work.util.CheckArray;
/**
* 题目描述:3. 请实现一个函数:最长顺子;输入很多个整数(1<=数值<=13),
* 返回其中可能组成的最长的一个顺子(顺子中数的个数代表顺的长度);
* 其中数字1也可以代表14; 顺子包括单顺\双顺\3顺;单顺的定义是连续5个及以上连续的数,
* 比如1,2,3,4,5、3,4,5,6,7,8和10,11,12,13,1等;双顺的定义是连续3个及以上连续的对(对:两个相同的数被称为对),
* 比如1,1,2,2,3,3、4,4,5,5,6,6,7,7和11,11,12,12,13,13,1,1等;3顺的定义是连续2个及以上连续的3张(3张:3个相同的数被称为3张),
* 比如1,1,1,2,2,2、3,3,3,4,4,4,5,5,5,6,6,6和13,13,13,1,1,1等等;
*比如:输入数组[1,5,2,3,4,4,5,9,6,7,2,3,3,4], 输出数组[2,2,3,3,4,4,5,5]
* @author 陈砚庭
*
*/
public class Work_ThreeSequence implements Work_Three{
private int[] tempCount=null;
public static void main(String[] args) {
Work_Three longest=new Work_ThreeSequence();
Integer[] array = { 1, 5, 2, 3, 4, 4, 5, 9, 6, 7, 2, 3, 3, 4 ,1,13,13,13,11,11,11,12,12,12,1,2};
try {
if(CheckArray.checkArray(array))
System.out.println(longest.getLongest(array));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<ArrayList<Integer>> getLongest(Integer[] array)throws Exception{
if(array==null){
throw new NullPointerException();
}
Arrays.sort(array);//对数组进行排序
ArrayList<ArrayList<Integer>> listOne=null;
ArrayList<ArrayList<Integer>> listTwo=null;
ArrayList<ArrayList<Integer>> listThree=null;
try{
listOne = getLongest(array, 1);
listTwo = getLongest(array, 2);
listThree= getLongest(array, 3);
int list1Length=listOne.get(0).size();
int list2Length=listTwo.get(0).size();
int list3Length=listThree.get(0).size();
if(list2Length>list1Length){
listOne=listTwo;
}
if(list3Length>list1Length){
listOne=listThree;
}
}catch(Exception e){
throw new NullPointerException();
}
return listOne;
}
/**
* 获得数组中的顺子
* @param array
* @param count
* @return
*/
private ArrayList<ArrayList<Integer>> getLongest(Integer[] array, int count) throws NullPointerException,IllegalArgumentException{
if(array==null){
throw new NullPointerException();
}
if(count<1){
throw new IllegalArgumentException();
}
tempCount=new int[14];
//取得每个数字在数组中出现的个数
for(int i=0;i<array.length;i++){
tempCount[array[i]]++;
}
ArrayList<Integer> longest = new ArrayList<Integer>();//记录最长的顺子
ArrayList<Integer> temp = new ArrayList<Integer>();//记录当前的顺子
ArrayList<ArrayList<Integer>> longests=new ArrayList<ArrayList<Integer>>();//记录最长的顺子,不分大小
for (int i = 0; i < array.length;i+=tempCount[array[i]]) {
int start = array[i];
if (getCount(start, count)) {
for (int j = 0; j < count; j++) {
temp.add(start);
}
} else {
continue;
}
while (start <= 13) {
if (getCount(start + 1, count)) {
for (int j = 0; j < count; j++) {
if (start + 1 == 14) {
temp.add(1);
} else {
temp.add(start + 1);
}
}
} else {
break;
}
start++;
}
if (temp.size()>longest.size()) {
longest = (ArrayList<Integer>) temp.clone();
longests.clear();
longests.add(longest);
}else if(temp.size()==longest.size()){
if(!temp.equals(longest)){
longests.add(temp);
}
}
temp = new ArrayList<Integer>();
}
return longests;
}
/**
* 在数组中查找n是否出现count次
* @param n 寻找的次数
* @param count 出现的次数
* @return
*/
private boolean getCount(int n, int count) {
if (n == 14) {
n = 1;
}
if(tempCount
>=count){
return true;
}else{
return false;
}
}
}

package www.work.util;
public class Work_FourArrayFile {
private int index = 0;// 数组长度
private String[] files = null;
public synchronized void add(String file) {
index++;
String[] tempFiles = new String[index];
if (files == null) {
tempFiles[0] = file;
} else {
tempFiles[tempFiles.length - 1] = file;
System.arraycopy(files, 0, tempFiles, 0, files.length);
}
files = tempFiles.clone();
}
public synchronized void remove(String file) {
if (index < 0) {
throw new IllegalArgumentException();
}
index--;
String[] tempFiles = new String[index];
int indexTemp = 0;
for (int i = 0; i < files.length; i++) {
if (files[i] == file) {
indexTemp = i + 1;
continue;
}
}
if (indexTemp != 0) {
if (indexTemp != 1) {
System.arraycopy(files, 0, tempFiles, 0, indexTemp - 1);
}
if (indexTemp != files.length) {
System.arraycopy(files, indexTemp, tempFiles, indexTemp - 1,
files.length - indexTemp);
}
files = tempFiles.clone();
}
}
public int getIndex() {
return index;
}

public String[] getFiles() {
return files;
}
/*
* public void clear() { files=null; }
*/
public static void main(String... args) {
Work_FourArrayFile work_FourArrayFile = new Work_FourArrayFile();
int[] temp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] index = new int[9];
System.arraycopy(temp, 1, index, 0, 9);
for (int i = 0; i < index.length; i++) {
System.out.println(index[i]);
}
work_FourArrayFile.add(100 + "");
work_FourArrayFile.add(101 + "");
work_FourArrayFile.add(102 + "");
work_FourArrayFile.add(103 + "");
work_FourArrayFile.add(104 + "");
work_FourArrayFile.add(105 + "");
work_FourArrayFile.add(106 + "");
work_FourArrayFile.add(107 + "");
work_FourArrayFile.add(108 + "");
work_FourArrayFile.add(109 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(100 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(101 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(102 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(103 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(104 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(105 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
work_FourArrayFile.remove(106 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(107 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(108 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(109 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println("sdfd");
}
}
package www.work.util;

import java.util.HashMap;
import java.util.Map;
public class Work_Fourutil {
private Work_Fourutil(){}
private static Map<String, Long> maps = new HashMap<String, Long>();
public static void put(String arg0,Long arg1){
maps.put(arg0, arg1);
}
public static Map<String, Long> getMaps() {
return maps;
}
}
package www.work.service;
import java.util.Map;
public interface Work_Four {
/**
* 从指定目录下读取文件
* @param path
* @return
* @throws Exception
*/
public Map<String, Long> readProject(String path)throws Exception;
}
package www.work.service.impl;
import java.util.Map;
import www.work.service.Work_Four;
import www.work.util.Work_FourArrayFile;
import www.work.util.Work_Fourutil;
/**
* 4.请设计一个程序:使用多线程,统计程序源代码行数;源代码是可以编译通过的合法的代码, 统计其物理总行数、其中的空行行数、其中含有有效代码的行数、
* 其中含有注释内容的行数;(要求必须利用多线程编程, 如果代码框架能更容易的扩展到支持多种语言的源代码行数统计,将获得更高的评价。)
*
* @author Administrator
*
*/
public class Work_FourImpl implements Work_Four {
private Work_FourThreadFileImpl filesArray = null;
private Work_FourThreadLineIpml lineArray = null;
private boolean flag = false;
public static void main(String... args) throws Exception {
Work_Four w = new Work_FourImpl();
String path = "D:\\java\\test\\src\\main\\java\\www\\work\\impl";
Work_FourArrayFile wf_file=new Work_FourArrayFile();
Map<String, Long> maps = w.readProject(path);
for (String map : maps.keySet()) {
System.out.println(map + "=" + maps.get(map));
}
}
public Map<String, Long> readProject(String path) throws Exception {
// TODO Auto-generated method stub
exec(path);
while(filesArray.getWf_product().isLineflag()){
}
end();
return Work_Fourutil.getMaps();
}
private void end(){
Work_Fourutil.put("空格行", filesArray.getWf_product().getBlank_Lines());
Work_Fourutil.put("有效代码行", filesArray.getWf_product().getCode_Lines());
Work_Fourutil.put("注释行", filesArray.getWf_product().getComment_Lines());
Work_Fourutil.put("物理行", (filesArray.getWf_product().getBlank_Lines()+filesArray.getWf_product().getCode_Lines()+filesArray.getWf_product().getComment_Lines()));
}
private void exec(String path) {
Work_FourArrayFile wf_file=new Work_FourArrayFile();
Work_FourProduct wf_product=new Work_FourProduct(path,".java",wf_file);
//获取文件目录的线程
filesArray=new Work_FourThreadFileImpl(wf_product);
Thread thread1 = new Thread(filesArray);
//统计文件的线程
lineArray = new Work_FourThreadLineIpml(wf_product);
Thread thread2 = new Thread(lineArray);
thread1.start();
thread2.start();
}
}
package www.work.service.impl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import www.work.util.Work_FourArrayFile;
public class Work_FourProduct {
private Work_FourArrayFile wf=null;
private boolean fileflag = true;// 标志位,判断获得文件路径的线程是否运行完成
private boolean lineflag = true;
public boolean isFileflag() {
return fileflag;
}
public void setFileflag(boolean fileflag) {
this.fileflag = fileflag;
}
private String path=null;//要遍历的文件路径
private String suffix=null;//文件后缀
private long code_Lines = 0; // 可执行代码行数
private long comment_Lines = 0; // 注释行数
private long blank_Lines = 0; // 空格行数
public long getCode_Lines() {
return code_Lines;
}
public long getComment_Lines() {
return comment_Lines;
}
public long getBlank_Lines() {
return blank_Lines;
}
public Work_FourProduct(String path,String suffix,Work_FourArrayFile wf) {
super();
this.path = path;
this.suffix=suffix;
this.wf=wf;
}
public Work_FourArrayFile getWf() {
return wf;
}
public boolean isLineflag() {
return lineflag;
}
public void setLineflag(boolean lineflag) {
this.lineflag = lineflag;
}
public synchronized void execSetWf() throws Exception{
while(true){
if(wf.getIndex()>0){
this.wait();
}else{
break;
}
}
readsFile(path,suffix);
this.notify();
}
private void readsFile(String path,String suffix) throws Exception {
// TODO Auto-generated method stub
File f = new File(path);
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
readsFile(child.getPath(),suffix);
} else if (child.getName().matches(".*\\"+suffix+"$")) {
wf.add(child.getPath());
}
}
}
public synchronized void execGetWf() throws InterruptedException{
while(true){
if(wf.getIndex()<=0){
this.wait();
}else{
exec();
this.notify();
break;
}
}
}
private void exec() {
String[] files = wf.getFiles();
if (files != null&&files.length>0) {
for (String f : files) {
try {
readFileLines(new File(f));
wf.remove(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void readFileLines(File f) throws Exception {
BufferedReader br = null;
boolean tempFlag = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim(); // 除去注释前的空格
if (line.matches("^[ ]*$")) { // 匹配空行
blank_Lines++;
} else if (line.startsWith("//")) {
comment_Lines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
comment_Lines++;
tempFlag = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
comment_Lines++;
} else if (tempFlag == true) {
comment_Lines++;
if (line.endsWith("*/")) {
tempFlag = false;
}
} else {
code_Lines++;
}
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
throw e;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package www.work.service.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import www.work.util.Work_FourArrayFile;
/**
* 此类用于读取指定目录下的java源文件
* @author 陈砚庭
*
*/
public class Work_FourThreadFileImpl implements Runnable {
public static void main(String ...args){
String path="D:\\java\\test\\src\\main\\java\\www\\work\\impl";
Work_FourArrayFile wf_file=new Work_FourArrayFile();
Work_FourProduct wf_product=new Work_FourProduct(path,".java",wf_file);
Work_FourThreadFileImpl w=new Work_FourThreadFileImpl(wf_product);
Thread t=new Thread(w);
t.start();
//System.out.println(Work_FourArrayFile.getFiles());
}
public Work_FourProduct getWf_product() {
return wf_product;
}
private Work_FourProduct wf_product=null;
public Work_FourThreadFileImpl(Work_FourProduct wf_product) {
super();
this.wf_product=wf_product;
}
public void run() {
// TODO Auto-generated method stub
while(wf_product.isFileflag()){
try {
wf_product.execSetWf();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
wf_product.setFileflag(false);
System.out.println("Work_FourThreadFileImpl线程执行完毕!!!!");
}
}
}
}
package www.work.service.impl;
/**
* 此线程类用于保存行数
*
* @author Administrator
*
*/
public class Work_FourThreadLineIpml implements Runnable {
private Work_FourProduct wf_product = null;
public Work_FourThreadLineIpml(Work_FourProduct wf_product) {
this.wf_product = wf_product;
}
public void run() {
// TODO Auto-generated method stub
while (wf_product.isLineflag()) {
try {
wf_product.execGetWf();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
wf_product.setLineflag(false);
System.out.println("Work_FourThreadLineIpml线程执行完毕!!!!");
}
}
}
}

通过这次的面试,明显的知道了自己的不足,以后要多往算法复杂度方面去思考问题,上面的方法都是可以改进的。

本文出自 “陈砚羲” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息