某公司数据库面试题

Question 1:
---------------------------------------------------------------------------------------
写一个函数 把传入的数组倒置 可以用任何编程语言
不能用现有函数,除了coun或者size之类的基本操作

Question 2:
---------------------------------------------------------------------------------------
In database XYZ, there's a table 'run_key' with the following rows:

idbatch last_processed
------------------------------
1  5           999
26           999
3  7             0
4  5             0
5 6           999
6  7             0
7  5             0
8  6             0
97           999
107             0


Write a SQL statement to return the number of rows with last_processed=0 for each batch.
The expected output is:

batchcount
-------------
5            2
6            1
7            3

Question 3:
---------------------------------------------------------------------------------------
In table 'info', there's a column named 'graduation_date' of type date. Some rows have 
incorrect graduation_date such as '1907-11-09' and '1908-03-17'; they should be 
'2007-11-09' and '2008-03-17'. 
Write a SQL statement to update all the rows with graduation_date < '1909-01-01' by 
setting graduation_date to '20yy-mm-dd'.
For example, '1907-11-09' updated to '2007-11-09', and '1908-03-17' updated to '2008-03-17'.

Question 4:
---------------------------------------------------------------------------------------
in case you are not familiar with regular expression matching, here's a quick example:

if we have a string "<year>2009</year>" and we want to extract '2009', we can use
/<year>(.*)<\/year>/.

based on the example, write ONE regular expression that will match "BALTIMORE COUNTY" in 
"<p><strong>BALTIMORE COUNTY</strong></p>" AND "NEW YORK"
in "<p><strong>NEW YORK</strong></p>"

Question 5:
---------------------------------------------------------------------------------------
in case you are not familiar with regular expression substitution, here's a quick tutorial:

the syntax of string substitution is:
VARIABLE =~ s/SEARCH_PATTERN/NEW_STRING/

For example,
$a = 'abc,123';
$a =~ s/(\w+),(\w+)/\2,\1/; # $a is now '123,abc' because \1='abc' and \2='123'

Here's the question:
write ONE substitution statement(ie. s/SEARCH_PATTERN/NEW_STRING/) so that
"<date>1999-02-25</date>" will be updated to "<date>02-25-1999</date>" AND

"<date>2005-11-03</date>" will be updated to "<date>11-03-2005</date>"


Question 6:
---------------------------------------------------------------------------------------
learn the concept of 'hash table' first. then solve this:

array 1 has some integers (for example: 1, 3, 5, 7) and array 2 has some integers (for example: 8, 5, 6, 1).

write a function to find the integers that exist in both arrays.


Question 7:
---------------------------------------------------------------------------------------
mysql> select * from a;
+--------+
| letter |
+--------+
| x      |
| y      |
| z      |
+--------+
mysql> select * from b;
+--------+
| letter |
+--------+
| a      |
| b      |
| y      |
+--------+

write a query to return letters that exist in both table a and table b;
write a query to return letters that exist in table a but not in table b.

Question 8:
---------------------------------------------------------------------------------------
写一个函数 传入一个数组和N 要求把前N个元素移到最后 需要占用最少内存


比如传入[a b c d e], N=2, 要求返回数组[c d e a b]



答案:(稍后)

answer1:

下面是我用java语言写的一维数组和二维数组的转置程序。
一维数组的转置:

 

package arraytest;

public class ArrayInversion1 {

public static void main(String[] args) {

int[] a={2,3,4,5};

arrayInversion(a);

for(int i:a){

System.out.println(i);

}

}

//一位数组倒置

public static void arrayInversion(int[] a){

int temp;

for(int i =0;i<=(a.length)/2;i++){

temp = a[i];

a[i]=a[a.length-i-1];

a[a.length-i-1] =temp;

}

}

}




二维数组的转置:

package arraytest;

public class ArrayInversion2 { 

    public static void main(String[] args) {  

     //二维数组可以不规则

   

        int a[][]={{1,2,3},{4,5,6},{7,8}};

        

        show(a);

     

        int[][] rea =arrayInversion(a);

          

        show(rea);

          

    }  

    

    

    //倒置方法

     public static int[][] arrayInversion(int[][] a){

     //创建临时变量count,记录数组中长度最大的数组length

        int count =0;

          

        for(int i = 0;i

       

        if(count

        count = a[i].length;

        }

        }

        

       // System.out.println("max"+count);

        

        //创建数组用来装载倒置的数组数据

        int rea[][]=new int[count][a.length];

       

        //倒置

        for(int i=0 ; i

            for(int j=0; j

                rea[j][i]=a[i][j];   

            }     

        }  

   

    return rea;

    }

    

     

     //输出数组

    public static void show(int[][] a){

        for (int x[]:a){  

            for(int e:x){  

                System.out.print(e+" ");  

            }  

            System.out.println();  

        }  

        System.out.println();  

    }

  

}  


 

answer2:

 

select batch,count(batch) as count from run_key where last_processed=0 group by batch;


 

 

你可能感兴趣的:(数据库)