1. 程式人生 > >java高階工程師開放面試題集<一>

java高階工程師開放面試題集<一>

 

臨近年關,不少人蠢蠢欲動,有童鞋問我java後端面試會面試什麼?

作為一個java後端老鳥,跌打滾爬多次被面試和麵試別人,總結了一些經驗,希望對大家有所幫助。

特別說明,僅僅針對工作兩年以上的java後端開發。以開發性題目為主。

1.資料結構相關

  假設1億整數存放在一個txt檔案中,如何去重和排序?

  思路:

  1.1.面試者要評估一下一億整數的大小。一個int佔4個位元組,1億呢?

  1.2.去重的資料結構有哪些?HashSet--->引申到HashMap--->ConcurrentHashMap 

  1.3 資料量增大到十億百億怎麼去重?

  布隆過濾器,優點,缺點

  1.4.其他方式?

    資料庫distinct order by,txt怎麼匯入到資料庫?load

         redis去重排序,redis的資料結構-->引申到其他資料結構 String,list,hash,set,sorted set,hyperloglog,geo

        mongo去重排序,

        ....

2. 演算法相關,主要考察程式碼能力

   斐波那契數列(fabnacci)實現,首先介紹一下該演算法的思想

  

    2.1 第一級別實現: 兩層遞迴

     public static long fibonacci(int n){
           if(n==0) return 0;
           else if(n==1) return 1;
           else 
           return fibonacci(n-1)+fibonacci(n-2);
           } 

問演算法複雜度及評估一下效能問題,提示可以優化。

    2.2 第二級別:減少一層遞迴

    public static void main(String[] args) {
        long tmp=0;
        // TODO Auto-generated method stub
        int n=10;
        Long start=System.currentTimeMillis();
        for(int i=0;i<n;i++){
            System.out.print(fibonacci(i)+" ");
        }
        System.out.println("-------------------------");
        System.out.println("耗時:"+(System.currentTimeMillis()-start));
    }    

public static long fibonacci(int n) {
        long result = 0;
        if (n == 0) {
            result = 0;
        } else if (n == 1) {
            result = 1;
            tmp=result;
        } else {
            result = tmp+fibonacci(n - 2);
            tmp=result;
        }
        return result;
    }

  問題,演算法複雜度,引導有沒有不用遞迴的?

     2.3 無遞迴

    public static long fibonacci(int n){
        long before=0,behind=0;
        long result=0;
        for(int i=0;i<n;i++){
            if(i==0){
                result=0;
                before=0;
                behind=0;
            }
            else if(i==1){
                result=1;
                before=0;
                behind=result;
            }else{
                result=before+behind;
                before=behind;
                behind=result;
                
            }
        }
        return result;
    }

3.併發問題

   給出一個普通的spring mvc controller,如下:

@Controller
public class WelcomeController {

    private final Logger logger = LoggerFactory.getLogger(WelcomeController.class);
    
  @Autowired
  private final HelloWorldService helloWorldService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Map<String, Object> model) {

        logger.debug("index() is executed!");

        model.put("title", helloWorldService.getTitle(""));
        model.put("msg", helloWorldService.getDesc());
        
        return "index";
    }

}

   問題:

    3.1.執行緒模型是什麼?單執行緒

    3. 2.如何提升qps?執行緒池 executor

    3.3.線上程池下如何控制併發?訊號量Semaphore或者計數器CountDownLatch

       引申到:Java中的可重入鎖:synchronized 和 java.util.concurrent.locks.ReentrantLock

   

4.資料庫相關

    場景:一張表 test(a,b,c,e,f,g) 100w記錄  常用查詢條件 ab  abc  abe,如何提升查詢效率?

    4.1.索引,

    4.2.複合索引的規則:最左原則。查詢條件ae走不走索引?

    4.3 1000w,1億,十億以上條記錄查詢是否會有什麼不同?

     4.4 多執行緒下如何保證資料一致性?樂觀鎖/悲觀鎖,應用場景不同點

 

5.設計模式相關

   

public class Test {  
  
    @Test  
    public void test() throws InterruptedException, ExecutionException {  
        AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("sys.out");  
        Future<String> future = executor.submit(new OutThread());  
        System.out.println(future.get());  
        System.out.println("Hello, World!");  
        Thread.sleep(10000 * 1000L);  
    }  
      
    static class OutThread implements Callable<String> {  
  
        public void run() {  
              
        }  
  
        @Override  
        public String call() throws Exception {  
            String ret = " i test callable";  
            for (int i = 0; i < 10; i++) {  
                    try {  
                            Thread.sleep(2 * 1000L);  
                            System.out.println("i sleep 1s");  
                    } catch (InterruptedException e) {  
                         // TODO Auto-generated catch block  
                         e.printStackTrace();  
                    }  
             }  
            return ret;  
        }    
    }  
}  

    5.1 看程式說明

    5.2 引申到reactor模型

         spring reactor

          vert.x

          akka

    5.3 servlet 3 響應式程式設計

太累,先寫到這裡吧。