iOS Intermediate Interview Questions

Half a year ago, I browsed a post on Zhihu about how to interview an iOS engineer . Since the company was recruiting iOS engineers at that time, I also interviewed many classmates, so I posted my list of interview questions with great interest, but unexpectedly attracted a lot of attention. There are also many students who answered seriously and privately messaged me the answers, and some asked to hide the level :(. Due to the busy work, I did not reply to the private messages and comments. Here is a summary reply: p The meaning of an interview question - I read all the private messages and more than 100 comments I received 

.

  • A student who just graduated is looking for a job interview recently, and wants to brush up the questions to increase the pass rate of the interview.
  • Students with certain work experience want to test their iOS level and see how much they can sell.
  • I have a good foundation in iOS, and I am supplementing my knowledge with the mentality of having many skills and not overwhelming myself.
  • The boss of the local tyrant is short of a programmer, and he wants to find a true love with interview questions with standard answers.

Except for the third type of students who have a correct mentality, the others all overestimate the role of interview questions. The interview questions are just martial arts moves, and the knowledge system is the inner skill. At the beginning, all the moves are memorized, but there are so many moves, and there will always be omissions and blind spots during the interview. The inner strength and mental method are all-in-one, and can respond to all changes with the same. If you can't answer all the interview questions, it doesn't mean you failed iOS, and if you answer all the questions correctly, you can't go to heaven. What should really be paid attention to is the theoretical knowledge system behind this question. There are many other high-quality answers in the post, covering various aspects such as candidate mentality, habits, basic knowledge, product understanding, etc., which are worth reading. Of course, since there is a question, there must be an answer, and there must be a target group. The main objects of investigation are students who have been engaged in iOS development for 1 to 3 years. It is not necessary to answer all the questions correctly, but it is good to be able to speak eloquently on more than half of the questions.


All kinds of comment area

In the comment area, there are all kinds of fairy complaints, some say it is difficult, some say it is too easy, and there are also artists and Android parties. Everyone's discussions were mixed, but it can be seen that many students have incorrect mentality. The road of technology is endless, and the expansion of breadth and depth requires years of accumulation. If there is nothing that is enough, just check it when you use it. There is no need to understand it so deeply. The technician's vision and stamina determine how far you can go on this road. The following types of students named and criticized:

  • Pretending to be ignorant is clever.
  • I feel that sqlite is too heavy and unnecessary to use.
  • It is too easy not to answer the question.
  • Saying it won't do it, but it doesn't affect the project.
  • It is said that the rounded head portrait can be done by asking the artist to cut a picture.
  • It is unnecessary to delve into it halfway.

Mentality fails.


qualified answer

To my surprise, several students answered the questions correctly and privately messaged me the answers. Here is one of the answers that is not bad, followed by the respondent's own answer. Clear answer


my answer

I have had a lot of interview and interview experience. As an interviewer, the interview questions were never made to confuse the interviewer, but to understand the interviewer from multiple angles and comprehensively to build trust. The most worrying thing during the interview is the silence. The interview questions are just an introduction. From the bottom of my heart, I most hope that the interviewers I meet will be able to draw inferences. In addition to answering the questions themselves, they can also confidently quote from sources and talk about the principles behind them or related knowledge theories. The question itself is not so important. The questions in this list are not difficult. Here I list my answers and the answers I expect from my perspective.

What is arc? (What problem was arc created to solve?)
Nowadays, many programmers start directly from arc and have never been in touch with mrc. Their understanding of arc only stays at the level of apple's help in memory management. What I really want to know about this question is the understanding of memory management. Although retain release does not need to be written, there will still be bugs such as memory leaks and wild pointer crashes under arc. If you can explain the meaning of the birth of arc from the perspective of retain count, a memory management strategy, you are right. If you can pull off other types of strategies, such as mark and sweep in java, then add points and praise.

Please explain the difference between the following keywords: assign vs weak, __block vs __weak
This question is a basic grammar question, and the answer can be found online. However, many students really don't know that weak will be set to nil after the object is released. The understanding of the __block keyword is a little more difficult, because the meaning (the impact on retain count) is completely different under arc and mrc. After understanding these keywords, you can deal with the risk of introducing retain cycle when using block. This question is still within the scope of memory management.

使用atomic一定是线程安全的吗?
看这题的问法不用想答案肯定是NO。有些人说不出所以然,有些人知道通过property的方式使用才能保证安全,还有人知道这个用来做多线程安全会有性能损耗,更有出色的候选人能谈atomic,synchronized,NSLock,pthread mutex,OSSpinLock的差别。好奇宝宝点我

描述一个你遇到过的retain cycle例子。(别撒谎,你肯定遇到过)
说没遇到过的我很难相信你有过成熟项目的经历。这题答不出了会扣很多很多分。用过block,写过delegate的肯定都踩过坑。

+(void)load; +(void)initialize;有什么用处?
这题属于runtime范畴,我遇到过能说出对runtime的理解却不知道这两个方法的候选人。所以答不出来也没关系,这属于细节知识点,是加分项,能答出两个message各在什么阶段接收就可以了。

为什么其他语言里叫函数调用, objective c里则是给对象发消息(或者谈下对runtime的理解)
这题考查的是objective c这门语言的dynamic特性,需要对比c++这类传统静态方法调用才能理解。最好能说出一个对象收到message之后的完整的流程是如何的。对runtime有完整理解的候选人还能说出oc的对象模型。

什么是method swizzling?
说了解runtime但没听过method swizzling是骗人的。这题很容易搜到答案。定位一些疑难杂症bug,hack老项目实现,阅读第三方源码都有机会接触到这个概念。

UIView和CALayer是啥关系?
能答出UIView是CALayer的delegate就及格了,能说出UIView主要处理事件,CALayer负责绘制就更好,再聊下二者在使用过程中对动画流畅性影响的注意点就superb。UI流畅性是个大话题,推荐看下这两篇文章。中餐西餐

如何高性能的给UIImageView加个圆角?(不准说layer.cornerRadius!)
这题讨论的最多,还有说美工切图就搞定的。答主在项目里做过圆角头像的处理,里面的坑还真不少。cornerRadius会导致offscreen drawing有性能问题,美工切图无法适用有背景图的场景,即使加上shouldRasterize也有cache实效问题。正确的做法是切换到工作线程利用CoreGraphic API生成一个offscreen UIImage,再切换到main thread赋值给UIImageView。这里还涉及到UIImageView复用,圆角头像cache缓存(不能每次都去绘制),新旧头像替换等等逻辑。还有其他的实现方式,但思路离不开工作线程与主线程切换。

使用drawRect有什么影响?(这个可深可浅,你至少得用过。。)
不少同学都用过drawRect或者看别人用过,但不知道这个api存在的含义。这不仅仅是另一种做UI的方式。drawRect会利用CPU生成offscreen bitmap,从而减轻GPU的绘制压力,用这种方式最UI可以将动画流畅性优化到极致,但缺点是绘制api复杂,offscreen cache增加内存开销。UI动画流畅性的优化主要平衡CPU和GPU的工作压力。推荐一篇文章:西餐

ASIHttpRequest或者SDWebImage里面给UIImageView加载图片的逻辑是什么样的?(把UIImageView放到UITableViewCell里面问更赞) 
很多同学没有读源码的习惯,别人的轮子拿来只是用用却不知道真正的营养都在源代码里面。这两个经典的framework代码并不复杂,很值得一读。能对一个UIImageView怎么通过url展示一张图片有完整的理解。涉及到的知识点也非常多,UITableViewCell的复用,memory cache, disk cache, 多线程切换,甚至http协议本身都需要有一定的涉及。

麻烦你设计个简单的图片内存缓存器(移除策略是一定要说的)
内存缓存是个通用话题,每个平台都会涉及到。cache算法会影响到整个app的表现。候选人最好能谈下自己都了解哪些cache策略及各自的特点。常见的有FIFO,LRU,LRU-2,2Q等等。由于NSCache的缓存策略不透明,一些app开发者会选择自己做一套cache机制,其实并不难。

讲讲你用Instrument优化动画性能的经历吧(别问我什么是Instrument)
Apple的instrument为开发者提供了各种template去优化app性能和定位问题。很多公司都在赶feature,并没有充足的时间来做优化,导致不少开发者对instrument不怎么熟悉。但这里面其实涵盖了非常完整的计算机基础理论知识体系,memory,disk,network,thread,cpu,gpu等等,顺藤摸瓜去学习,是一笔巨大的知识财富。动画性能只是其中一个template,重点还是理解上面问题当中CPU GPU如何配合工作的知识。

loadView是干嘛用的?
不要就简单的告诉我没用过,至少问下我有什么用。。这里是apple给开发者自己设置custom view的位置。说UI熟悉的一定要知道。

viewWillLayoutSubView你总是知道的。。
controller layout触发的时候,开发者有机会去重新layout自己的各个subview。说UI熟悉的一定要知道。

GCD里面有哪几种Queue?你自己建立过串行queue吗?背后的线程模型是什么样的?
两种queue,串行和并行。main queue是串行,global queue是并行。有些开发者为了在工作线程串行的处理任务会自己建立一个serial queue。背后是苹果维护的线程池,各种queue要用线程都是这个池子里取的。GCD大家都用过,但很多关键的概念不少人都理解的模凌两可。串行,并行,同步,异步是GCD的核心概念。

用过coredata或者sqlite吗?读写是分线程的吗?遇到过死锁没?咋解决的?
没用过sqlite是说不过去的。用过CoreData的肯定有很多血泪史要说。多谢线程模型你肯定做过比较选择。死锁是啥肯定也是要知道的,没遇到过至少能举个简单的例子来说明。单个线程可以死锁(main thread里dispatch_sync到main queue),多个线程直接也可以死锁(A,B线程互相持有对方需要的资源且互相等待)。

http的post和get啥区别?(区别挺多的,麻烦多说点)
这个可以说很多。不希望听到的答案有

  • 两个差不多,随便用一个。
  • post比get安全(其实两个都不安全)

能说下两个http格式有什么不同,各自应用的场景就合格了。更多可以阅读下这个答案

我知道你大学毕业过后就没接触过算法数据结构了,但是请你一定告诉我什么是Binary search tree? search的时间复杂度是多少?我很想知道!
很多人都很排斥数据结构和算法题,我个人意见是复杂的可以不知道,基础的一定要了解。时间复杂度是什么得知道,list,queue,stack,table,tree这些都要明白是啥。连hash表的概念都不知道怎么能保证在写代码的时候注意性能呢。


隐藏关卡

其实当初写这份答案的时候并没有准备什么隐藏关卡,只不过有一些从自己这些年项目经历里总结出来的有深度的知识点,感觉可以难倒不少同学:p。求隐藏关卡的同学真不少,近期我会再准备一份进阶版面试题,权当作隐藏关卡。面向的对象是3~5年iOS开发经验的同学。再次申明下:这只是一份面试题。

https://toutiao.io/posts/184340/app_preview

Guess you like

Origin blog.csdn.net/args_/article/details/52796258