preface

Every winter, every year also can’t stop an Android programmer to pursue the determination of big factory. What knowledge points do we need to master if we want to enter the big factory? Here, I sort out an overall knowledge framework for you. Overall including Java, Android, algorithm, computer foundation and so on, the corresponding knowledge points of the interview questions are sorted out. I hope you can improve and organize your own knowledge system after reading. I wish you all an early entry into their ideal company ~~

The Java part

1.StringBuilder

  • StringBuffer threads are safe, StringBuilder threads are not
  • + is actually implemented with StringBuilder, so you can just use + for acyclic bodies, not for loop bodies, because stringBuilders are created frequently
  • StringBuilder < StringBuffer < concat < + StringBuilder < StringBuffer < concat < +

2. Generic erase

  • Structure-related generics such as modifier member variables are not erased
  • The container class generics are erased

3. The Exception and the Error

  • Exceptions and errors both inherit from Throwable
  • Error mostly refers to an unrecoverable Error state, such as OOM, so you don’t need to catch it
  • Exception is classified into CheckedException and UnCheckedException. CheckedException: must be explicitly caught and checked by the compiler, for example, UnCheckedException for IO operation: Do not show captures, such as null Pointers, array overbounds, etc

IO, NIO, OKIO

  • IO is stream-oriented, processing one byte at a time, while NIO is buffer-oriented, generating or consuming one chunk of data at a time
  • IO is blocking, NIO is non-blocking
  • NIO supports memory mapping
  • Okio has a much simpler API than IO and NIO
  • Okio supports timeouts
  • Okio introduced ByteString space swap time to improve performance
  • Okio uses segment mechanism for memory sharing to save copy time

5. ArrayList, and LinkedList

  • ArrayList

Array-based implementation, fast search: O (1), slow add and delete: O (n) The initial capacity is 10, expansion through the System. ArrayCopy method

  • LinkedList

Based on bidirectional linked list, slow search: O (n), fast add and delete: O (1) encapsulates the call of queue and stack

6.HashMap, HashTable, HashSet

  • HashMap (allows key/value to be null)

Based on arrays and one-way linked lists, arrays are the body of a HashMap; The linked list is designed to resolve hash collisions. It contains an array of entities that combine keys and values, indexed by key.hashCode (and then hash twice). In JAVA 8, when the number of linked lists is greater than 8, it is converted to red-black tree storage. The search time is changed from O(n) to O(logn). The array length is always 2 ^ n: In this way, the bit operation can be used to implement the mod, so that the index can fall within the length of the array. The loading factor (0.75 by default) indicates the number of fill ratios to be added for expansion. Small fill ratio: the linked list is short and the length of the array is twice as long as that of the original array. Then the original object is hash to find the new index and put it back

  • HashTable (key/value not allowed to be null)

Data structures are as thread-safe as hashMaps

  • HashSet

Based on the HashMap implementation, the element is the key of the HashMap, and the Value is passed a fixed Value

7. Principle of Synchronized

Each object has a monitor lock: Monitorenter starts, motnitorexit ends Wait/notify relies on monitor, So in the synchronized code block execution will quote IllegalMonitorStateException anomalies

8.Minor GC/Major GC/Full GC

  • Minor GC (Young GC) : That is, garbage collection of the new generation (divided into one Eden zone and two Survivor zones), useless objects in Eden zone will be collected, and the surviving objects in Survivor zone will be copied to another Survivor zone. The replication times are also recorded as the age. If the Survivor region is Full, the surviving object will be moved to the old age (prematurely promoted). If the old age cannot accommodate, Full GC will be triggered (promoted failed). The old age object may reference the new age object, so this reference will be used as GC Roots
  • Major GC: Usually equivalent to Full GC, reclaiming the entire heap
  • Full GC: Reclaims the entire heap, both new generation and old generation when space is allocated in the old generation but cannot accommodate it

9.JVM memory structure

  • Thread private: 1. Program counter: records the address of the bytecode instruction being executed, null if Native method is being executed 2. Virtual machine stack: When executing a method, the data required by the method is stored as a stack frame and pushed onto the stack. Native method stack: Same as virtual machine stack, but for Native methods
  • Thread sharing: 1. Heap: Stores Java instances, GC main area, generation collection GC method will divide the heap into new generation, old generation 2. Method area: store class information, constant pool, static variables and other data

10.GC

  • Recycle area: only for heap and method area; Thread private area data is destroyed at the end of the thread without recycling
  • Collection types: 1. Objects in the heap: Generation collection The GC method divides the heap into new generation and old generation. New generation: New objects will enter new generation. Objects are reclaimed by a copy algorithm; Old age: New large objects and old objects will enter old age. Objects are reclaimed by a mark-sweep algorithm. 2. Class information and constant pool in the method area
  • To determine whether an object can be recycled: 1. Reference counting: has the disadvantage of circular reference 2. Reachability analysis: Search from GC ROOT, unreachable objects can be reclaimed. GC ROOT includes objects referenced in the virtual machine stack/local method stack and objects referenced by constant/static variables in the method area.

Due to the limited space of this article, in order not to affect your reading experience, only part of the explanation of some interview questions, more interview questions + detailed explanation【 Click on me 】Get it free.

Android development

1.Android class loader

In Android development, whether plug-in or component, are based on the Android system ClassLoader ClassLoader to design. But the Android platform virtual machine runs Dex bytecode, a product of class file optimization, the traditional class file is a Java source file will generate a. Class file, and Android is all the class file merge, optimization, The purpose is to keep only one copy of the things repeated by different class files. In early Android application development, if the Android application is not divided into dex, the apK of the last application will only have one dex file.

There are two common class loaders in Android, DexClassLoader and PathClassLoader, which inherit from BaseDexClassLoader. The difference is that when the parent constructor is called, DexClassLoader passes an additional parameter, optimizedDirectory, which must be the internal storage path used to cache the Dex files created by the system. For PathClassLoader, the parameter is null and only the Dex file of the internal storage directory can be loaded. So we can use DexClassLoader to load external APK files, which is the basis of many plug-in technologies.

2.Service

To understand the Android Service, you can understand it from the following aspects:

  • Services are executed on main threads. Time-consuming operations (network requests, copying databases, and large files) cannot be performed on services.
  • You can set the process of a Service in XML and have it execute in another process.
  • A Service performs a maximum of 20 seconds, a BroadcastReceiver 10 seconds, and an Activity 5 seconds.
  • An Activity is bound to a Service by bindService (Intent, ServiceConnection, flag).
  • An Activity can start a Service using startService and bindService.

IntentService

IntentService is an abstract class that inherits from Service and contains a ServiceHandler (Handler) and HandlerThread (Thread). IntentService is a class that handles asynchronous requests. Within an IntentService there is a worker thread (HandlerThread) to handle time-consuming operations. IntentService is started as usual, but when the task is completed, IntentService will stop automatically. IntentService can also be started multiple times, with each time-consuming operation executed as a work queue in the onHandleIntent callback of IntentService, one worker thread at a time. IntentService is essentially an asynchronous framework that encapsulates HandlerThread and Handler.

Life cycle diagram

As one of the four components of Android, Service is widely used. Like activities, services have a series of lifecycle callback functions, as shown in the figure below.

Generally, there are two ways to start a Service, startService and bindService.

3. Create method of fragemnt

First, we need to create an XML file, and then create the corresponding Java file, which is associated with the return method of onCreatView(). Finally, we need to configure related parameters in the Activity, that is, put the fragment location in the XML file of the Activity.

 <fragment
        android:name="xxx.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </fragment>
Copy the code

(2) Dynamic creation

There are several steps to dynamically create a Fragment:

Create the Fragment instance to be added. Get FragmentManager, can be directly in the Activity by calling getSupportFragmentManager () method. Start a transaction by calling the beginTransaction() method. Adding or replacing a fragment to a container is usually done using the repalce() method, which requires passing in the id of the container and the fragment instance to be added. Commit the transaction and call commit() to do so.

Adapter contrast

The FragmnetPageAdapter only separates fragments when switching pages. The FragmnetPageAdapter is suitable for fragments with a few pages to save some memory and has no significant impact on system memory. The FragmentPageStateAdapter recollects fragments each time the page is switched. It is suitable for fragments with many pages and does not consume much memory

4.Activity lifecycle

A typical life cycle is when an Activity goes through the normal life cycle of creating, running, stopping, and destroying with the user involved.

onCreate

This method is called back when the Activity is created. It is the first method called in the lifecycle. When creating an Activity, we usually need to override this method, and then do some initialization operations in this method, such as setting the resources of the interface layout through setContentView, and initializing the required component information.

onStart

This method is called back to indicate that the Activity is being started and that the Activity is already visible, but not yet displayed in the foreground, so it cannot interact with the user. The Activity is displayed and we can’t see the pendulum.

onResume

When this method is called back, the Activity is visible in the foreground and ready to interact with the user (in the Active/Running state described above). The same thing between onResume and onStart is that both indicate that the Activity is visible. The onStart callback does not allow the Activity to interact with the user in the background, whereas the onResume callback is displayed in the foreground and can interact with the user. The onResume method is also called when the Activity stops (onPause and onStop are called) and comes back to the foreground, so we can also initialize some resources in the onResume method. Such as reinitializing resources released in onPause or onStop.

onPause

This method is called back to indicate that the Activity is stopped (Paused), and normally the onStop method will be called back soon after. If the onResume method is immediately executed after the onPause method is executed, the onResume method will be called back to the current Activity. Of course, we can do some data storage or animation stops or resource reclaiming in onPause, but this should not be too time-consuming, as it may affect the display of the new Activity — the onResume method of the new Activity will not be executed until the onPause method is finished.

onStop

When the Activity is Stopped or completely overwritten, it is not visible and only runs in the background. Similarly, the onStop method allows you to do some resource release (not too time-consuming).

onRestart

Indicates that the Activity is being restarted. This method is called back when the Activity changes from invisible to visible. In this case, when the user starts a new Activity, the current Activity is paused (onPause and onStop are executed), and when the user returns to the current Activity page, the onRestart method is called back.

onDestroy

At this point the Activity is being destroyed and is the last method to execute in the lifecycle, so we can usually do some recycling and the final resource release in this method.

5. DecorView analyses

For example, a DecorView is the top-level View of the entire Window interface, which has only one child LinearLayout. Represents the entire Window interface, including the notification bar, title bar, content display bar three areas. The LinearLayout has two FrameLayout children.

The role of DecorView

A DecorView is a top-level View that is essentially a FrameLayout and it has two parts, the title bar and the interior bar, both of which are FrameLayout. The interior column ID is content, the part of the activity that sets the setContentView, and finally adds the layout to the FrameLayout id content. Get content: ViewGroup Content =findViewById (Android.id.content) Get the set View: getChildAt(0).

Used to summarize

Each Activity contains a Window object, which is typically implemented by PhoneWindow. PhoneWindow: Sets the DecorView as the root View of the entire application Window, which is the Window implementation class. It is the most basic window system in Android. Each Activity creates a PhoneWindow object, which is the interface between the Activity and the View system. DecorView: Is the top-level View that renders the specific content to be displayed on the PhoneWindow. The DecorView is the ancestor of all views of the current Activity and does not present anything to the user.

6. Event distribution of View

The event distribution mechanism of a View can be represented as follows:

As shown in the figure above, the figure is divided into three layers: Activity, ViewGroup and View from top to bottom.

  1. Events start with the white arrow in the upper left corner and are dispatched by the Activity’s dispatchTouchEvent
  2. The top word of the arrow represents the method return value (return true, return false, return super.xxxxx(),super means to call the superclass implementation.
  3. The dispatchTouchEvent and onTouchEvent boxes have the word “true—-> consume” in them, which means that if the method returns true, the event will be consumed and will not go anywhere else, and the event will terminate.
  4. At present, all graph events are for ACTION_DOWN. We will analyze ACTION_MOVE and ACTION_UP at last.
  5. The Activity dispatchTouchEvent in the previous diagram was wrong (figure fixed), only the return super.dispatchTouchEvent(ev) was further down, and the event returned true or false was consumed (aborted).

7. The View of the map

In an XML layout file, our layout_width and layout_height parameters can be wrap_content or match_parent instead of the actual size. These two Settings do not specify the actual size, but the View we draw on the screen must have a specific width and height, and for this reason we have to handle and set the size ourselves. Of course, the View class provides the default processing, but if the default processing of the View class does not meet our requirements, we will have to rewrite the onMeasure function.

The onMeasure function is an int that contains the measurement mode and size. Ints take up 32 bits. Google uses the first two bits of ints to distinguish between different layout patterns, and the last 30 bits to store the size of the data. The use of onMeasure function is shown as follows:

MeasureSpec has three measurement modes:

Match_parent – > EXACTLY. How do you understand that? Match_parent is using all the free space that the parent View gives us, and the free space that the parent View gives us is determined, which is the size of the integer in this measurement mode.

Wrap_content – > AT_MOST. We want to set the size to wrap our view content, so the size is the parent view gives us as a reference size, as long as it does not exceed this size, the specific size according to our needs to set.

Fixed size (e.g. 100DP) — >EXACTLY. Users specify their own size, we do not have to interfere, of course, with the specified size mainly.

8. Distribute ViewGroup events

When a click event is generated, it is passed in the following order:

Activity -> Window -> View

Events are always passed to the Activity, which in turn is passed to the Window, which in turn is passed to the top-level View, which, upon receiving the event, distributes the event according to the event distribution mechanism. If a View’s onTouchEvent returns FALSE, its parent’s onTouchEvent will be called, and so on, and the Activity will handle the event if nothing else.

For ViewGroup event distribution, it looks something like this: If the top-level ViewGroup intercepts the event (onInterceptTouchEvent returns true), the event will be handled by the ViewGroup. If the onTouchListener of the ViewGroup is set, onTouch will be called. Otherwise onTouchEvent will be called, that is: if both are set, onTouch will block onTouchEvent, and in onTouchEvent, if onClickerListener is set, onClick will be called. If the top-level ViewGroup does not intercept, the event will be passed to the child view of the clicked event, and the child view’s dispatchTouchEvent will be called

9. The ViewGroup

Customizing a ViewGroup is not easy, because it needs to take care of both its own and its child views. We all know that a ViewGroup is a View container that holds the Child View and is responsible for putting the Child View into the specified location.

First of all, we need to know the size of each child View. Only by knowing the size of each child View can we know how big the current ViewGroup should be set to accommodate them.

The size of the ViewGroup depends on the size of the subview and what our ViewGroup is going to do

Once the size of the ViewGroup and subview is calculated, the next step is to put it, how to put it? It’s up to you to customize it. For example, if you want the child views to be placed next to each other in vertical order, or one on top of the other in chronological order, it’s up to you.

Already know how to put ah, decided how to put is equivalent to the existing space “divided” into large and small space, each space corresponding to a sub-view, we next is to put the sub-view into the seat, put them in their place.

Due to the limited space of this article, in order not to affect your reading experience, only part of the explanation of some interview questions, more interview questions + detailed explanation【 Click on me 】Get it free.

Data structures and algorithms interview questions

1. What are the commonly used data structures?

An array of 2.

(1). How to find the missing number in an array of integers from 1 to 100 (2). How do I find duplicate numbers in a given array of integers? (3). How to find the maximum and minimum values in an unsorted integer array? (bytedance) (4). How do I remove multiple copies from a given array in Java? (5). Big Numbers add up (Today’s headlines)

3. The linked list

(1). How about the first query and the second-to-last query? The first node is given to the first node, and the second to last node needs to be queried from the last to the last. How do I find the median of a single linked list in a single traverse? (Ping an of China) (4). How to prove whether a given linked list contains a loop? How do I find the head node of the loop? (youku) (5). Two crossed single linked list, find the intersection point (Huawei) (6). How do I get the length of a single linked list? (360) (7). How to reverse single linked lists without recursion? (xiaomi/Meituan) (8). How to judge the link list has a ring? (details)

Queue & stack

(1). How to use stack to realize the function of queue (Guangzhou Litchi FM) (2). Two stacks to realize a queue (Mushroom Street) (3). Two queues implement a stack (Tencent) (4). Compare the queue and stack, and their bottom implementation (Tencent)

5. Binary tree

(1). How to perform preemptive traversal in a given binary tree? (2). How to implement the post-order traversal algorithm? (3). How to perform binary search in a given array? Given that the foreorder traversal is {1,2,4,7,3,5,6,8} and the middle order traversal is {4,7,2,1,5,3,8,6}, what is its binary tree? (5). Input two binary trees A and B to determine whether B is A substructure of A. Please implement two functions to serialize and deserialize binary trees (YY) respectively (7). What’s the difference between a balanced binary tree and a red-black tree? (bytedance) (8). What is balanced binary tree and what are its characteristics (Meituan) (9).B tree, B+ tree

6.HashMap

(1). What is the underlying principle of HashMap? Is thread safe? (2). How to implement put in HashMap? (3). Talk about when the hashMap needs to be expanded, and how to expand resize(). (4). What is hash collision? How to solve it? (5). The difference between HashMap and HashTable (xiaomi) (6). When does HashMap need to be expanded and how does resize() be implemented? Hashmap concurrenthashMap principle (Meitan) (8). Arraylist and HashMap are different, why is it faster? (Bytedance)

7. Figure

(1). Rotate the output matrix (2). Given a matrix int matrixA[m][n], each row and each column are incremented, implement an algorithm to find some element in the matrix element. sogou

8. What are the sorting algorithms?

9. Search algorithms

String 10.

Computer network section

1.HTTP 2.TCP/IP 3.TCP three-way handshake and four-way handshake The entire process from entering the URL to rendering the entire interface, and what protocol is used in the process? 5. What is the difference between TCP and UDP? 7. The format of HTTP request and response packets, and common status code 8. How many HTTP requests can be sent over a TCP connection

Due to the limited space of this article, in order not to affect your reading experience, only part of the explanation of some interview questions, more interview questions + detailed explanation【 Click on me 】Get it free.