A collection of written questions (puzzles)

Source: Internet
Author: User
Tags repetition table definition

First, Jane answers

1. Advantages and disadvantages of dynamic link library and static link library

2. What is the difference between polling task scheduling and preemptive scheduling?

3. List the locks commonly used in the database and their application scenarios

Second, the algorithm design problem

1. Given n is a positive integer, the smallest "non-heavy complex" of the greater than N, where the non-repetition means that there are no two equal adjacent bits, such as 1102 in 11 is equal to two adjacent bits so it is not the number of repetitions, and 12301 is the number of repetitions.

2. Set n is a large integer, and the longest palindrome substring of the string of length n is obtained.

3. The point on the axis from left to right is a[0], a[1], a[2]......a[n-1], the length of a stick is L, I can reach a few points of the axis?

Third, the system design problem

1. In the modern system design process, in order to alleviate the request pressure, usually adopts the cache technology, in order to further enhance the cache hit ratio, the common use distribution is the cache scheme. The dispatch module provides services to users who request different content to assign to different cache servers. Please give a distributed cache scheme to meet the following requirements:

1) Single cache server failure, the entire distributed cache cluster, can continue to provide services.

2) through a certain allocation strategy, you can ensure the full use of each cache service storage space, and load balancing. When some servers fail or the system expands, the allocation policy can guarantee a smaller cache file redistribution overhead.

2016 Baidu campus Recruitment pen questions selected

3) When there is a difference in storage space for different cache servers, the allocation policy can satisfy the proportional allocation.

Here are some of my own answers, not guaranteed 100% correct, welcome criticism.

First, Jane answer 1. Advantages and disadvantages of dynamic link libraries and static link libraries

Answer: (1) Dynamic Linked Library: Windows provides rich function calls for applications that are included in the dynamic-link library. There are 3 of the most important dll,kernel32.dll, User32.dll and GDI32.dll. There are two ways to use it: static loading, which is loaded when the application starts, and dynamic loading, that is, the dynamic link library is loaded by the application when it is used. The advantages are as follows:

A. Sharing: Multiple applications can use the same dynamic library, when launching multiple applications, only need to load the dynamic library into memory once;

B. Development module good: requires the designer to divide the function better.

The disadvantage is that you cannot resolve issues such as reference counting.

(2) Static Library: Functions and data are compiled into a binary file (usually with an extension of. LIB). In the case of a static library, when compiling the linked executable, the linker copies the functions and data from the library and combines them with other modules of the application to create the final executable file (. EXE file). The static link library is linked at compile time as part of the code. Advantages and disadvantages are as follows:


Article 2016 Baidu campus recruitment pen questions selected from Http://www.gkstk.com/article/wk-78500000920999.html, reproduced please keep this link!

The code is loaded fast and executes faster, because it only links the part you need when compiling, and the application is relatively large. However, if multiple applications are used, they will be loaded multiple times, wasting memory.

2. What is the difference between polling task scheduling and preemptive scheduling?

Answer: (1) The principle of polling scheduling is to each time the request from the user to the internal server, starting from 1, until N (the number of internal servers), and then restart the loop. Other tasks (including high-priority tasks) are allowed to control the CPU only if the current task actively abandons the CPU control, such as a task hangs. Its advantage is its simplicity, which does not need to record the state of all current connections, so it is a stateless dispatch. But is not conducive to the subsequent request in a timely manner to get a response.

(2) Preemptive scheduling allows a high-priority task to interrupt the currently executing task and seize control of the CPU. This facilitates the subsequent high-priority tasks to be responded to in a timely manner. But the task of achieving relatively complex and potentially low-priority tasks is not scheduled for long.

3. List the locks commonly used in the database and their application scenarios

Answer: The lock in the database is a very important concept in the network database, it is mainly used in the multi-user environment to ensure the integrity and consistency of the database. The basic theory of locks used in various large-scale databases is consistent, but there are differences in concrete implementation. Currently, most database management systems are more or less self-regulating, self-managing functions, so many users actually do not know the theory of lock and the implementation of the lock in the database. When locking in the database, in addition to the different resources can be locked, you can also use different degrees of lock mode, that is, there are many modes of lock, SQL Server lock mode includes:

1) shared lock

In SQL Server, shared locks are used for all read-only data operations. Shared locks are non-exclusive and allow multiple concurrent transactions to read their locked resources. By default, when data is read, SQL Server immediately releases the shared lock. For example, when you execute a query "select * from My_table", first lock the first page, after reading, release the lock on the first page, and then lock the second page. This allows you to modify the first page that is not locked during a read operation. However, the Transaction Isolation level connection option settings and the lock settings in the SELECT statement can change this default setting for SQL Server. For example, "SELECT * from My_table HOLDLOCK" requires that the table be locked during the entire query process until the query is complete before releasing the lock.

2) Modify the lock

Modify locks are used during the initialization phase of the modify operation to lock resources that might be modified, thus avoiding the deadlock caused by the use of shared locks. Because a shared lock is used, the operation to modify the data is divided into two steps, first obtaining a shared lock, reading the data, and then upgrading the shared lock to an exclusive lock before performing the modification operation. This way, if two or more transactions simultaneously request a shared lock on a transaction, the transactions are promoted to an exclusive lock when the data is modified. At this point, these transactions do not release the shared lock but wait for the other party to release, which creates a deadlock. A deadlock can be avoided if a data is directly requested to modify the lock before it is modified and then upgraded to an exclusive lock when the data is modified. Modifying a lock is compatible with a shared lock, which means that a resource is locked with a shared lock, allowing it to be locked again with a modified lock.

3) Exclusive lock

An exclusive lock is reserved for modifying data. The resources it locks, other transactions cannot be read, and cannot be modified. An exclusive lock cannot be compatible with other locks.

4) Structure lock

Default classification

Structural locks are divided into structural modification locks (sch-m) and structural stability Locks (sch-s). When you perform a table definition language operation, SQL Server takes a SCH-M lock and SQL Server uses a sch-s lock when compiling the query.

5) Intent Lock

Intent locks indicate that SQL Server has the intention of acquiring a shared or exclusive lock on the lower level of the resource. For example, a table-level shared intent lock describes a transaction intent to release an exclusive lock to a page or row in a table. The intent lock can also be divided into shared intent lock, exclusive intent lock and shared exclusive intent lock. A shared intent lock describes the transaction intent to place a shared lock on a low-level resource locked by a shared intent lock to read data. An exclusive intent lock indicates that the transaction intends to modify the data by placing an exclusive lock on the low-level resource to which the shared intent lock is locked. Shared exclusive lock description transactions allow other transactions to use shared locks to read top-level resources and intend to place an exclusive lock on the lower layer of the resource.

6) Batch Modify lock

Bulk-Modify locks are used when bulk copying data. Bulk modification locks can be set by TABLOCK hints on the table or by using the "table lock on bulk load" option sp_tableoption the system stored procedure.

Second, the algorithm design problem

1. Given n is a positive integer, the smallest "non-heavy complex" of the greater than N, where the non-repetition means that there are no two equal adjacent bits, such as 1102 in 11 is equal to two adjacent bits so it is not the number of repetitions, and 12301 is the number of repetitions.

Algorithm thought: Of course the most direct method is the use of violence law, starting from n+1 1 to determine whether the number is not repeated, is to exit the loop output, this method is generally undesirable, such as n=11000000, you want to add 1 to 12010101, a total cycle million, It is undesirable to repeat the judgment every time for the number of repetitions and the inefficiency. Here I use the method is: from the top of the n+1 to the right to determine whether it is equal to the next high, if found equal (that is, the number of repetitions) of the secondary high plus 1, note that there may be carry, such as 8921―>9021, followed by a direct set of 010101 ... form, such as 1121―>1201, at which time the initial construction of "not heavy complex" is completed, but at this time the "non-plural" is not necessarily the true non-repeating number, because it is possible that the secondary high after rounding becomes 0 or 00 after rounding, such as 9921―> 10001, this time need to re-cycle to determine the structure to meet the conditions can be, this method of cycle very little, I think not more than 3 times can meet the conditions.


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1850813

A collection of written questions (puzzles)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.