×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

a sample java implementation

This is just one solution for your reference

	public static void printMatrix(int [][] a, int row, int col){
		int minK = rowk; j--) System.out.print(a[row-1-k][j]+" ");
			for(int i=row-k-1; i>k; i--) System.out.print(a[i][k]+" ");
		}
		System.out.println();
	}
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / ice (甲基苯丙胺) 贡献的一个算法问题,大家讨论
    其实都是很简单的data structure的应用。给你一个题吧 by ice (甲基苯丙胺) at 2006.3.2 00:24 [#746553@9]

    one singly linked list has two heads (h1 and h2)
    the linked list is structured like:
    1(h1)->2->3->4->5->6.......->n
    (n+1)(h2)->(n+2)->...->(n+m)->i (i<n)

    given h1 and h2, return the merged node's address.
    • 不明白.从i->n的节点算不算merged?
      • 返回第一个就可以了。
    • :S
    • Forgot to mention, do it IN PLACE and don't modify the given linked list.
    • 打听一下,这种类型的问题是不是面试经常会考啊?还是你们纯粹是有兴趣?
      • a true interview question
      • 牛人也对C有兴趣? #2818323, #2818310
        • 第一,这是个算法问题不是个C问题;第二,我的兴趣在于面试是不是会考这种东西,而不在这个问题本身;第三,每个人都有自己牛的领域和不牛的领域,算法领域我是低到水平面以下,大学的东西都忘光光了
          • 和你说笑,别往心里去.
    • 再来一个
      Design an algorithm to print a 2 dimensional array in a spiral clockwise manner? e.g. A[3][4] = 1 2 3 4 5 6 7 8 9 10 11 12 Print : 1 2 3 4 8 12 11 10 9 5 6 7
      • 这个题好像比较容易。
        B[3][4]=0,0,0,0,0,0,0,0,0,0,0,0


        x=0;
        y=0;
        while (1)
        {
        print A[x][y];
        b[x][y]=1;
        if (y<3 && b[x][y+1] == 0 )
        y++
        else if ( x<2 && b[x+1][y] ==0 )
        x++
        else if ( y>0 && b[x][y--] ==0 )
        y--
        else if ( x>0 &&b[x-1][y]==0 )
        x--
        else
        break
        }
        • Dude, u have to assume it's a m*n matrix
          • 你把2,3 换成m-1,n-1不行吗?
            • 正在消化你的算法呢。你那个中间辅助数组可能面试者不答应。另外,你最好implement一次,看是否工作。
              • 你总要有个东西保存某节点是否已经走过的状态。
                • 这个问题我其实是今天上午才整出来,可惜rolia不支持html posting 了,要不俺可以贴俺做的原码
                  • 可以贴html阿?贴出来交流交流。
                    • a sample java implementation

                      This is just one solution for your reference

                      	public static void printMatrix(int [][] a, int row, int col){
                      		int minK = rowk; j--) System.out.print(a[row-1-k][j]+" ");
                      			for(int i=row-k-1; i>k; i--) System.out.print(a[i][k]+" ");
                      		}
                      		System.out.println();
                      	}
                      
                      • 7, <pre> tag doesn't work
                        public static void printMatrix(int [][] a, int row, int col){
                        int minK = row<col?row:col;
                        for(int k=0; k<minK; k++){
                        for(int j=k; j<col-k; j++) System.out.print(a[k][j]+" ");
                        for(int i=k+1; i<row-k; i++) System.out.print(a[i][col-1-k]+" ");
                        for(int j=col-k-2; j>k; j--) System.out.print(a[row-1-k][j]+" ");
                        for(int i=row-k-1; i>k; i--) System.out.print(a[i][k]+" ");
                        }
                        System.out.println();
                        }
                        • 不错,是个好方法。
              • 另外如果一定不让用辅助数组,一个简单方法是A[x][y]=-A[x][y]; 改条件为 A[][]>0 ; 不过看不出有什么必要。
    • Write code to reverse a linked list
      • 用不着reverse link list吧。先沿 h1走一边,把所有指针存在另一个数租里,在沿h2走,每一步都在数组里查一遍,找到的第一个就是了。
        • usually they will ask you to do it IN PLACE.
          • that's the point! even u have done recusive solution, then they'll ask nonrecursive solution.
          • 怎样才算IN PLACE呢? 正确解法是什么?
            • for example swap number a and b in place. like prototype: void swap(int a, int b). IN PLACE means no extra memory should be used.
              • 照这么说这题就没法做了,不允许使用额外变量。没有临时指针,比都没法比。不过我误会你了,看来你说"reverse a linked list"是另一道题。
                • 这样吧,你先实现swap(a,b) in place.介个比较好整一点。算法肯定有。
                  • 这我初中就会了.
                    • please give the algorithm
                      • 时间太久,我忘了行吗?
                        • It's really a good excuse to tell the interviewer.
                          • :-) 我想把一些通常见到的面试算法都做出来,建一个BLOG,最好有人能反馈改进,要不就么得什么意义了
                            • remember what i told you yesterday?
                              • em, then how can I improve my skills?三个臭皮匠抵个诸葛亮不是?
                                • 完全看态度
                          • 不过说实话,很多算法平时工作中根本用不上。几个大巨头的面试真的是变态!
                            • 其实,ms我不知道,amazon我有同学。上次去seattle一起出去喝酒,那厮整晚都在若有所思的想他的一个task。用perl处理什么东西。很多面试题都是他们实际工作中遇到的真正问题。
                              • 那他们岂不是用面试来套算法了?K,这么便宜的好事。Amazon的面试比MS的更切合实际一些,Google的更好。比如叫应试者写garbage collector是他们的问题之一。不过现在他们的面试题不允许外漏了。
                                • i was asked to figure out how spell checker works by amazon
                                  • wow! 你有好的组合算法吗?比如ABCDE里任选两个组合给打印出来?
                                    • what's the output? all possible two letters or you just need a random tow letters combination.
                                      • all two letters combinations, like ABCD => AB AC AD BC BD CD
                                        • If need to know how many combinations ------>n! / x!*(n-x)!
                                          • not the number of combinations but the coms themself
                                        • Easy way is something like bubble sort. Good way is something like merge sort. Instead of comparation, simply print them out. No difference
                          • 见intervirewer时说不定急中生智,就想起来了。
                            • 得,俺帮你想起来吧
                              void swap (int a, int b) {
                              a = a^b;
                              b = a^b;
                              a = a^b;
                              }
                              • 还真看不懂。^什么意思?
                                • Bitwise operators: &=AND |=OR ^=XOR ~=NOT
                                  • 高人就是喜欢故意让人不明白。a+b, a-b,a-b不就完了。
                                    • 哈哈,不错,终于想起来了。a+b, a-b,a-b的做好了,他们会问:能优化算法吗?
                                      • 靠,这才真叫变态,一个加法还要优化。
                                        • 当然变态!不过他们是Rule的设计者,玩不玩由你
          • 想出一个in place的解法:先h1走一遍,边走边拆,把指针都置成null,再h2走,走不动就是了。不过找完了,链表也没了。呵呵,这会不会把面试管气晕过去?
            • Forgot to mention, do it IN PLACE and don't modify the given linked list. -ice(甲基苯丙胺); 3.3 17:06 (#2817520@0)
            • very creative though. This problem has both recursive and non-recursive solutions
              when I was asked this question I used stack to solve it. I did get some points.
              • i guess you are talking about reversing a linked list. he is talking the one with two heads
              • 你是在说第一题吗?最笨的法子,先从h1走,一个一个跟h2比,没找到就h2走一步,再从h1一个一个比,不过这好像傻慢阿。应该有什么可以优化的地方吧。
                • 俺搞错了,你话题接在reverse link list下面,我以为你是说第二题。第一题you are on right track.
      • step1: Pcur = node0 ; Pnxt1= Pcur->next; Pnxt2 = Pnxt1->next; Prev = Pcur; step2: while(){ Pcur = Pnxt1; Pnxt1 = Pnxt2; Pnxt2 = Pnx2->next ; Pcur->next = Prev; Prev = Pcur }
        • There should have some condition check
        • The basic idea is look forward 2 positions, have one pointer for current node and a pointer to the reserve node
          • For recursive way. Break the linked list form the middle like the merge sort; Instead of merge them together, relink them in a reevise way
    • All the questions here are real interview questions.
    • write code to copy a binary search tree.
      • It depends on sallow or deep copy. If it's sallow copy, you only need copy the pointer. Copying a collection in both java and C++ are sollow copy. If it's a deep copy, it depends on what language you're choosing
        • think of serializing and deserializing it. so it's deep copy.
      • A language using array to implement BST, you need calculate the size of the array first then copy the whole chunk of the array. If use linked list, you need do a traverse to copy the linked list
        • When I say copy the linked list, I mean copy it into an array
    • 大家共同讨论,共同提高:-)
      如果我们以前多做一些这种基本训练,我们很多同志就不会被微软,亚马逊,古狗折磨了
    • implement a function that prints all possible ordering of the chars in a string.
      • similiar to merge sort or quick sort. the Big O time should be nlg(n)
        • interesting idea
    • implement a function that prints all possible combinations of the chars in a string.
    • 注意:每个问题最长时间约一小时。有感到简单的DX,如果你还在小公司的话,应该挑战MS, Google
      • These're practical problems. The real challenge is algorithm. You can always refine your design, but without talent you can't solve the algorithm problem. I personally find difficulties on algorithm
    • design a thread pool
      • This one is not easy. It's a OS related problem. It really test you how to implemet how to write a Semaphor and scheduler
    • design a garbage collector
      • step1: Create a deamon which gonna scan the objects on the heap and free any objects without referencing( the refernce counter is 0 )
      • step 2 create a base class with a date member, reference counter and initially the counter will be one.
      • If you have the change seeing some sections of open source code, this is the most common way to clean the memory
      • step 3, whenever you create a new class, extends the base class we defined in step2
      • step 4 Anytime any object is referenced, increment the counter. Anytime any object is released, decrease the counter.
        • great! new JVM uses different scheme. instead of reference counter, tracing collector is in favor
          • Java make it more complicated. There's difference between soft delete and hard delete. Anyway, it just another way to manage memory
            • any good memory management articles or books to recommand?
              • Sorry, I only collect thoes information piece by piece. I do learn a lot from C Programming
                • that's fine. Thanks anyway.
      • It also can be implemented by event driven in Java or by call back in C++. Issue an event or triger the call back when the object finds its counter becoming zero
    • design a memory management scheme
      • need more details
        • C++ way or Java way. Interviewer just want to know how you are familiar with any of them or even better all of them
          • If you really want to know it, it's more easy to be understand through assembly language. Have a look how the object created on heap, how a function is called and how the parameters are passed to the function
          • After you familiar thoes problems you can do futher research on how compiler( C or C++) or interpeter(java) works
        • After that you need focus on How OS works on memory allocation, how paging work and how context switch works. Memory management is really not only related to languages
    • design a can-juice vendor machine and test it
      • more details please
    • design an elevator control system and write test cases for it
      • There's an exact example with UML Diagram in Deitel and Deitel. No need snoop around, simply copy it
        • URL please
          • Pick a book like C++ How to program or Java How to program. They're pretty books. You need one of them on hand http://www.deitel.com/
    • Jig saw puzzle. What are the data structures? Assume you have some method which can tell you if two pieces fit together. How would you solve the puzzle, minimizing the number of times you have to call that function?
      • Where did you collect such a big number questions. It makes me fell like a final exam. The above questions are really good.
        • check ur PM
      • I alway tell people we're computer scientist not a damn programmer. Don't follow the trend but do some study on algorithm and OS. They're the core of CS. Sigh!!! No body believes me