[Int Basics] Coding & oo questions

Source: Internet
Author: User

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 'Madam": Mada m' I, madam

Example 2: Write function to compute nth Maid 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 to12x12

Java:(Similarfor 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 99108
10 20 30 40 50 60 70 80 100 110
11 22 33 44 55 66 77 88 99 110 121
12 24 36 48 60 72 84 96108 120 132

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 = newbufferedreader (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-digithexadecimal string.

Java:

Public String formatrgb (int r, int g, intb ){
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 java1.5:

Public String formatrgb (int r, int g, intb ){
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 between Ge (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? What about executable files? How do they model a directory containing program 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 showould 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 wocould 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

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.