码迷,mamicode.com
首页 > 其他好文 > 详细

[Int Basics] coding & OO questions

时间:2014-08-22 19:42:59      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:des   java   os   io   strong   for   ar   art   cti   

Example 1:   Write a function to reverse a string.

Example Java code:

    public static String reverse ( String s ) {
        int length = s.length(), last = length - 1;
        char[] chars = s.toCharArray();
        for ( int i = 0; i < length/2; i++ ) {
            char c = chars[i];
            chars[i] = chars[last - i];
            chars[last - i] = c;
        }
        return new String(chars);
    }

Example output for "Madam, I‘m Adam":   madA m‘I ,madaM

Example 2:  Write function to compute Nth fibonacci number:

Java and C/C++:

    static long fib(int n) {
        return n <= 1 ? n : fib(n-1) + fib(n-2);
    }

(Java Test harness)

    public static void main ( String[] args ) {
        for ( int i = 0; i < 10; i++ ) {
            System.out.print ( fib(i) + ", " );
        }
        System.out.println ( fib(10) );
    }

(C/C++ Test Harness)

    main () {
        for ( int i = 0; i < 10; i++ ) {
            printf ( "%d, ", fib(i) );
        }
        printf ( "%d\n", fib(10) );
    }

Test harness output:  

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Example 3:  Print out the grade-school multiplication table up to 12x12

Java: (similar for C/C++)

    public static void multTables ( int max )
    {
        for ( int i = 1; i <= max; i++ ) {
            for ( int j = 1; j <= max; j++ ) {
                System.out.print ( String.format ( "%4d", j * i ));
            }
            System.out.println();
        }
    }

Example output:

   1   2   3   4   5   6   7   8   9  10  11  12
   2   4   6   8  10  12  14  16  18  20  22  24
   3   6   9  12  15  18  21  24  27  30  33  36
   4   8  12  16  20  24  28  32  36  40  44  48
   5  10  15  20  25  30  35  40  45  50  55  60
   6  12  18  24  30  36  42  48  54  60  66  72
   7  14  21  28  35  42  49  56  63  70  77  84
   8  16  24  32  40  48  56  64  72  80  88  96
   9  18  27  36  45  54  63  72  81  90  99 108
  10  20  30  40  50  60  70  80  90 100 110 120
  11  22  33  44  55  66  77  88  99 110 121 132
  12  24  36  48  60  72  84  96 108 120 132 144

Example 4:  Write a function that sums up integers from a text file, one int per line.

Java:

    public static void sumFile ( String name ) {
        try {
            int total = 0;
            BufferedReader in = new BufferedReader ( new FileReader ( name ));
            for ( String s = in.readLine(); s != null; s = in.readLine() ) {
                total += Integer.parseInt ( s );
            }
            System.out.println ( total );
            in.close();
        }
        catch ( Exception xc ) {
            xc.printStackTrace();
        }
    }

Example 5:  Write function to print the odd numbers from 1 to 99.

C/C++:

    void printOdds() {
        for (int i = 1; i < 100; i += 2) {
            printf ("%d\n", i); // or cout << i << endl;
        }
    }

Java:

    public static void printOdds() {
        for (int i = 1; i < 100; i += 2) {
            System.out.println ( i );
        }
    }

Example 6:  Find the largest int value in an int array.

Java:

    public static int largest ( int[] input ) {
    int max = Integer.MIN_VALUE;
    for ( int i = 0; i < input.length; i++ ) {
        if ( input[i] > max ) max = input[i];
        }
        return max;
    }

Example 7:  Format an RGB value (three 1-byte numbers) as a 6-digit hexadecimal string.

Java:

    public String formatRGB ( int r, int g, int b ) {
        return (toHex(r) + toHex(g) + toHex(b)).toUpperCase();
    }

    public String toHex ( int c ) {
        String s = Integer.toHexString ( c );
        return ( s.length() == 1 ) ? "0" + s : s;
    }

Or in Java 1.5:

    public String formatRGB ( int r, int g, int b ) {
        return String.format ( "%02X%02X%02X", r, g, b );
    }

Example output for (255, 0, 128):   

 

Here are some examples:

  1. Design a deck of cards that can be used for different card game applications.

Likely classes: a Deck, a Card, a Hand, a Board, and possibly Rank and Suit. Drill down on who‘s responsible for creating new Decks, where they get shuffled, how you deal cards, etc. Do you need a different instance for every card in a casino in Vegas?

  1. Model the Animal kingdom as a class system, for use in a Virtual Zoo program.

Possible sub-issues: do they know the animal kingdom at all? (I.e. common sense.) What properties and methods do they immediately think are the most important? Do they use abstract classes and/or interfaces to represent shared stuff? How do they handle the multiple-inheritance problem posed by, say, a tomato (fruit or veggie?), a sponge (animal or plant?), or a mule (donkey or horse?)

  1. Create a class design to represent a filesystem.

Do they even know what a filesystem is, and what services it provides? Likely classes: Filesystem, Directory, File, Permission. What‘s their relationship? How do you differentiate between text and binary files, or do you need to? What about executable files? How do they model a Directory containing many files? Do they use a data structure for it? Which one, and what performance tradeoffs does it have?

  1. Design an OO representation to model HTML.

How do they represent tags and content? What about containment relationships? Bonus points if they know that this has already been done a bunch of times, e.g. with DOM. But they still have to describe it.

The following commonly-asked OO design interview questions are probably too involved to be good phone-screen weeders:

  1. Design a parking garage.
  2. Design a bank of elevators in a skyscraper.
  3. Model the monorail system at Disney World.
  4. Design a restaurant-reservation system.
  5. Design a hotel room-reservation system.

A good OO design question can test coding, design, domain knowledge, OO principles, and so on. A good weeder question should probably just target whether they know when to use subtypes, attributes, and containment. 

Example Data Structure questions:

1) What are some really common data structures, e.g. in java.util?

2) When would you use a linked list vs. a vector?

3) Can you implement a Map with a tree? What about with a list?

4) How do you print out the nodes of a tree in level-order (i.e. first level, then 2nd level, then 3rd level, etc.)

5) What‘s the worst-case insertion performance of a hashtable? Of a binary tree?

6) What are some options for implementing a priority queue?

[Int Basics] coding & OO questions

标签:des   java   os   io   strong   for   ar   art   cti   

原文地址:http://www.cnblogs.com/chayu3/p/3930102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!