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 響應式編程

太累,先寫到這里吧。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM