Array---interview knowledge point collation

Source: Internet
Author: User
Tags array definition array length

1, an ordered series, each value in the sequence can be divisible by 2 or 3 or 5, the initial value of this sequence starts from 1, but 1 is not in this sequence. What is a 1500th value?

Resolution: 2, 3, 5 of the least common multiple is 30. [1, 30] The number of qualified in 22. If you can see [31, 60] There are also 22 matching the number of conditions, the problem is easy to solve. In other words, these numbers are cyclical and have a period of 30. The 1500th number is: 1500/22=68 1500%68=4. That is, the 1500th number is equivalent to 68 cycles, and then the 4th number in a cycle is removed. First 4 numbers in a cycle: 2,3,4,5. Therefore, the result is 68*30=2040+5=2045.

2, sequential storage structure is generally continuous storage, and chain storage generally does not occupy continuous space, relatively, the storage density of sequential storage is large.

3. The most common operation of linear tables is to access the elements of any given sequence number and to insert and delete operations at the last, and to access them anywhere, the most time-saving effort is the array, which is the sequential table. And the element is in the final position to insert and delete operations, but also do not involve other elements to offset the extra operation, most of which is to expand the space

4, a 5*4 matrix, how many rectangles? (Square is also a rectangle)

Analysis: It can be understood that: five rows of four columns of the table has a 6*5 edge, from six sides to select two (horizontal), from four side two (portrait), you can determine a rectangle. C (6,2) *c (5,2) =15*10=150

5, linear structure refers to the logical structure, not the physical storage structure

Common linear structures are: linear tables, stacks, queues, double queues, arrays, strings.

Common nonlinear structures are: two-dimensional arrays, multidimensional arrays, generalized tables, trees (binary trees, etc.), graphs.

6, with a n row n column of the symmetric matrix A, the lower part of the triangle is stored in a one-dimensional array B, a[0][0] is stored in b[0], then the diagonal element of line I a[i][i] stored in B () place?

Parse: "Lower triangle", "a[0][0" store B[0] ", A[i][i] is the first i+1 row i+1 column element, is the 1+2+...+ (i+1) =[(i+1) * (i+2)/2] elements, also because the starting coordinate is 0. So it is stored in [(i+1) * (i+2)/2]-1=[(i+3) *I/2].

7, with an array of a[i,j], each element of the array length is 3 bytes, I the value of 1 to 8,j value is 1 to 10, the array from the memory of the first address BA in order to store, when used as the primary storage, element a[5,8] storage head address is ().

Resolution: Pay more attention to the starting value of i,j starting from 1

Amn, m corresponds to row, n corresponds to column

Line priority: LOC (AIJ) = loc (A11) +[(i-1) *n + j-1]*sizeof (Element)

Column precedence: loc (AIJ) = loc (A11) + [(j-1) *m + i-1]*sizeof (Element)

This question m = 8, n = 10; i = 5, j = 8

Column priority

LOC (AIJ) = ba + [(8-1) *8 + 5-1]*3 = ba + 180

8, there is a sparse matrix of 100*90, not 0 elements have 10, set each integer number of 2 bytes, then use ternary group to represent the matrix, the required number of bytes is ()

Parsing: Each element to be represented by the line number, column number, element value, in the ternary group to represent the sparse matrix, but also three members to remember, the number of rows of the matrix, the total number of elements, so the required number of bytes is 10* (1+1+1) *2+3*2=66

9, the array statically allocates memory, the list dynamically allocates memory;

The array is contiguous in memory, and the list is discontinuous;

Array elements in the stack area, linked list elements in the heap area;

The array uses subscript to locate, time complexity is O (1), the link list locates element time complexity O (n);

The time complexity of an array to insert or delete elements O (n), the time Complexity O (1) of the linked list.

10, in an array of the number of elements n, the optimal algorithm for finding the elements in ascending order of N/5 position is O (n), and the optimization time complexity of the number of K in an array of n is found to be O (n). The array of length k is partition, and the number of times to compare is K. Find the K-large number in an array of length n, worst case n-k times partition, with a complexity of n+n-1+n-2+...+n-k=kn+k (k+1)/2=o (n).

11, there are two from small to large sequence of arrays, the length of N and M, respectively, the two arrays into an ordered array of the minimum number of comparisons is?

Resolution: When the minimum number of comparisons is the case, is the minimum value of an array when it is greater than the maximum value of another array. The number of comparisons is the minimum value in M and N

The best is min (m,n), the worst is m+n-1

12. The operational performance of several common data structures, as shown in

13, the linear table of length n is ordered to find, in the worst case, the number of comparisons required is n

14. The difference between a linked list and an array.

Analysis: Linked list: The list is a discontinuous dynamic space, the length is variable, the list needs to retrieve the nodes sequentially, the efficiency is low;

The advantage of the list is that the nodes can be inserted and deleted quickly, the size is dynamically allocated, and the length is not fixed.

There is no cross-border problem with the linked list.

Array: An array is a fast, contiguous space that needs to be fixed for the length of the declaration.

The advantage of an array is that it is fast and the data operation uses the offset address directly.

Array has a cross-border problem.

15, the array usually has two basic operations is ()

Parsing: An array is a collection of data elements of the same data type stored in a contiguous storage space, where the value of the array element can be accessed by locating the storage address of the array element by the subscript (ordinal) of the array element. It is commonly used to implement sequential tables, typically including finding and modifying operations. Note, however, that the sequential table is a one-dimensional array, and the multidimensional array is a typical nonlinear structure.

It is also worthwhile to note that the array is characterized by:

1) The number of array elements is fixed, once the array is defined, there is no increment or decrement of the elements.

2) Data elements have the same type.

3) The subscript relation of an array has the upper and lower bounds and the subscript is ordered.

Therefore, arrays generally do not insert and delete operations

16. When storing a binary tree with a one-dimensional array, the nodes are always stored in a hierarchical traversal order.

17, sparse matrix in the use of compressed storage will lose the function of random storage. Because in this matrix, the distribution of non-0 elements is not regular, in order to compress the storage, the value of each non-0 element and its row, column number as a node stored together, such a node composed of a linear table called the ternary table, it is not a simple vector, so the subscript can not directly access the elements in the matrix.

18, Int (*p) [3] What is the meaning of P? A pointer to an int array with an element number of 3

Parsing: First look at the parentheses (because the parentheses have the highest precedence), (*p) stating that P must first be a pointer.

Then look back [3], said the pointer to an array, the array has three elements.

Look at the first int, which indicates that the array is integer.

Together, it is a pointer to an integer array with an element number of 3.

19. Delete element I in a sequential table of length n, to move n-i elements. If you want to insert an element before the first element, move the n-i+1 element back.

20, static linked list is used to store node data, simulation of the implementation of the list, but there is no use of pointers. Each array node consists of two parts: the data field and the cursor (cursor) field. Data is stored, and the cursor indicates the subscript of the next element in the array.

(1) When accessing the element I, it is necessary to traverse from the beginning to the i-1 and the element, by the cursor of the I-1 node, in order to know where the element i is stored, therefore, and I are related.

(2) The element is stored using an array, and the size is determined at the time of definition.

(3) The INSERT and delete operations do not need to move elements, only the cursor value can be modified, just as you would modify a pointer in a dynamic list.

21, about int a[10]; Which of the following addresses do not represent a[1]?

Resolution: A. a+sizeof (int)

Incorrect, equivalent to pointer operation A + 4 on a 32-bit machine

B. &a[0]+1

Correct, the array first element address plus 1, according to the pointer operation is a[1] Address

C. (int*) &a+1

Correct, the array address is cast to int*, then add 1, so that a meaning of B

D. (int*) ((char*) &a+sizeof (int.))

Correct, the data address is first converted to char*, then add 4, according to the pointer operation formula, moving forward 4 * sizeof (char), and then converted to int*, apparently a[1] address

22, with the alphabet sequence {q,d,f,x,a,p,n,b,y,m,c,w}, please write a two-way merge method for the sequence after a scan of the result is: DQFXAPBNMYCW

Parse: The order is as follows

{Q,D,F,X,A,P,N,B,Y,M,C,W}

Merge for the first time in two-way merging

{D, Q}  {F,x}  {A,p}  {B,n}  {M,y} {C,W}

Two-way Merge second merger

{D,f,q,x} {A,b,n,p} {C,m,w,y}

Merger of the two roads in the third time

{A,b,d,f,n,p,q,x} {C,m,w,y}

Merger of the second-way merge for the fourth time

{A,b,c,d,f,m,n,p,q,w,x,y}

So the answer is

Dqfxapbnmycw

23. The length of the generalized table ((a,b,c), d,e,f) is (1), the depth is (3)

Parsing: A generalized table is the list (lists) that we usually call. It relaxes the atomic limits on the table elements, allowing them to have their own structure.

The length of a generalized table: the number of commas in the maximum brackets +1; Length: Remove one layer of parentheses and the remainder is a few.

Depth of generalized table: the number of layers with parentheses after expansion. Depth: Remove several layers of parentheses to the last part.

24. Which of the following statements correctly declares a two-dimensional array of integers ()

Parsing: How two-dimensional arrays are defined:

1. int [] [] array = new INT[N][M];

2. int [] [] array={{1,2,3,4},{1,2,3,4},{1,2,3,4}};

Mode one: Defining a two-dimensional array can specify that no m is defined, but n must be defined.

25. What is the difference between an array pointer and an array of pointers?

Parsing: An array pointer (also called a row pointer) defines an int (*p) [n];() a high priority, first stating that P is a pointer to a one-dimensional array of integers, and that the length of the one-dimensional array is n, or the step of P. In other words, when executing p+1, p crosses the length of n integer data. To assign a two-dimensional array to a pointer, you should assign this value:

int a[3][4];

int (*p) [4]; The statement defines an array pointer to a one-dimensional array with 4 elements.

P=a; Assign the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]

p++; After the statement executes, that is, P=p+1;p crosses line a[0][] points to the line a[1][]

So an array pointer is also called a pointer to a one-dimensional array, also known as a row pointer.

pointer array definition int *P[N]; [] High priority, the first combination with P as an array, and then by int* that this is an integer pointer array, it has n pointer type array elements. The execution of p+1 here is wrong, so the assignment is also wrong: P=a, because P is an unknown representation, only p[0], p[1], p[2]...p[n-1], and they are pointer variables that can be used to hold variable addresses. But can be so *p=a; Here *p represents the value of the first element of the pointer array, the value of the first address of a. To assign a two-dimensional array to a pointer array:

int *p[3];

int a[3][4];

for (i=0;i<3;i++)

P[i]=a[i];

Here int *p[3] indicates that a one-dimensional array holds three pointer variables, respectively p[0], p[1], p[2]

So you have to assign values separately.

So the difference between the two is enlightened, the array pointer is just a pointer variable, it seems that the C language is specifically used to point to a two-dimensional array, it occupies memory of a pointer to the storage space. Pointer arrays are multiple pointer variables that exist in the form of an array of memory, occupying multiple pointers of storage space.

26, if there are 18 elements of the ordered table stored in a one-dimensional array a[19], the first element put A[1], now the binary lookup, then find a[3] comparison sequence of the subscript is ()

Parsing: Note that the first element is placed in a[1], a total of 18 elements, that is, a[1]~a[18].

First time: Low=1,high=18, mid=9 (9.5 downward rounding). A[3] and a[9] comparisons. Then the a[9] to the right of including a[9] all discarded.

Second time: A[1]~a [8]. Mid=4 (rounded down). Then a[4] the right side of the including a[4] all abandoned

Third time: a[1]~a[3]. mid=2. Then the a[2] to the left, including a[2] all discarded.

The fourth time; A[3] and a[3] comparison. Finally found you, fortunately I did not give up.

27, suppose to store three-dimensional array a[5][6][7] in order of precedence, where the address of element A[0][0][0] is 1100, and each element occupies 2 storage units, then a[4][3][2] address is (1482)

Parsing: three-dimensional array a[m1][m2][m3] if the first store in line, set a[0][0][0] The starting address is p, each element occupies n units, then the starting address of A[i][j][k] is:

LOC (i,j,k) =loc (i,0,0) + (j*m3+k) *n=loc (i-1,0,0) + (m2*m3+j*m3+k) *n=loc (i-2,0,0) + (2*m2*m3+j*m3+k) *n=...=p+ (i*m2*m3+j* M3+K) *n

Then LOC (4,3,2) =1100+ (4*6*7+3*7+2) *2=1482

28, 1) The data structure defines the table header and footer for a generalized table: if the generalized table ls= (a1,a2...an) is not NULL, then A1 is the table header of LS and the rest of the elements (A2,a3,.. An) is a footer called LS.

By definition, the header of a non-empty generalized table is an element that can be an atom or a child table, and a footer must be a child table. For example: Ls= (A, B), the table header is a, and the end of the table is (a) instead of B. In addition: Ls= (a) has a table header of a, and the footer is a blank table (). (2) Non-empty generalized table, except the table header, the rest of the elements constitute a table called the footer, so the non-empty generalized footer must be a table. A table header for a non-empty generalized table is an element that can be an atom or a child table, and a footer must be a child table.

29. There are two types of storage structures for linear tables:

1. Sequential storage structure---sequential table. The sequential table appears as an array and can be accessed by any subscript, so it is a random access storage structure.

2. Chained storage structure---linked list. The linked list appears as a linked list and must be accessed from the beginning, so it is a sequential access storage structure.

30, int *s[8]; Defines an array of pointers in which each element is a pointer, where each pointer points to a subsequent redefinition in the program.

int (*s) [8]; Defines an array pointer to a one-dimensional array with 8 elements (each element in the array is of type int).

differentiate int *p[n]; and int (*p) [n]; It depends on the precedence of the operator.

int *p[n]; , the operator [] has a high precedence and is first combined with p to be an array, and then by int* it is an integer pointer array.

int (*p) [n]; Medium () has a high priority, first stating that P is a pointer to a one-dimensional array of integers.

31, index Lookup is a lookup on the index table and the primary table (that is, the index storage structure of the linear table). The process of index lookup is: first, based on the given index value K1, find the Index table on the index of the index value equals K1, to determine the K1 corresponding child table in the primary table in the beginning and length, and then according to the given keyword K2, in the corresponding sub-table to find the keyword equals K2 element (node).

When searching an index table or a child table, if the table is an ordered table stored sequentially, both sequential lookups and binary lookups can be performed. Otherwise, only sequential lookups are possible.

Array---interview knowledge point collation

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.