[Int Basics] coding & OO questions

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?

时间: 2024-12-27 15:22:36

[Int Basics] coding & OO questions的相关文章

[Int Basics]

1, In your current role, tell me about a design decision you've had to make 2, Can you give me an example of a bad design decision you've made in the past. What was the impact 3, What's the most difficult technical problem you've had to solve 4, How

一份来自于全球的前端面试题清单,看看老外喜欢考哪些题(部分有答案)

方括号中的蓝色标题是题目的出处,有些题目在原址内包含答案.搜集的大部分外国前端面试题没有做翻译,单词并不难,大家应该看得懂.题目旁边的方括号内, 简单记录了与此题相关的知识点.总共大概一千多道,包含国内的题目,如有错误,欢迎指正.有些原链可能已无法打开,有些可能需要代理才能查看. 一.HTML [HTML related interview questions] 1.What is doctype? Why do u need it? 2.What is the use of data-* at

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen

(转) [it-ebooks]电子书列表

[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/Learning Web App Development || Build Quickly with Proven JavaScript Techniques http://

结对项目第一周总结

结对项目——四则运算 阶段性总结 一.需求分析(第一周达成): 能够生成n道四则运算题,n可由使用者输入来控制 支持整数 支持分数 生成题目中含有括号 可以判断正误,如果错误会输出正确答案 统计正确率 扩展需求: 生成题目后存入文件 完成题目后从文件读入并进行判断 支持题目去重 支持繁體中文,简体中文,English 二.设计思路(同时输出UML类图): 以下是程序主体设计思路,各步骤具体操作详情请见注释: 由于本周可用的准备时间较长,我们选择直接进行真分数的操作.在java语言中并没有一个专门

赫夫曼编译码器实验报告

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define MAX 100 #define MAXVALUE 10000 typedef struct{ char ch; int weight,flag; int parent,lchild,rchild; }HTNode; typedef struct{ char ch; int bit[MAX]; int

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

2017.9.5 考试

T1 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define ll long long 5 #define dd double 6 #define mem(a,b) memset(a,b,sizeof(a)) 7 using namespace std; 8 9 int n,intemp,maxp; 10 ll sum; 11 dd p[26]; 12 dd f[(1<<

【BZOJ】1027: [JSOI2007]合金(凸包+floyd)

http://www.lydsy.com/JudgeOnline/problem.php?id=1027 没special judge wa了一发好蛋疼... 首先这题大概和黑书上的差不多...由于知道任意两种材料就能得到第三种材料的含量,所以可以忽略第三种含量... 首先来看,如果有两种材料,那么能合成的材料一定在这个线段上,证明很简单... 假设材料A和B,要合成材料C,将他们材料的含量放到二维坐标系上(x轴为材料1,y轴为材料2) 假设用了a的A,则用了1-a的B,有 $$\begin{a