## Interview Practice 26 – String Right-rotation

Question Write a program to right-rotate a string by m characters. Right-rotating a string means moving m characters at the left of string to the right. Is it required the time complexity is O(n) and helper memory size is O(1). For example, right-rotate “abcdefghi” by 3 characters gives “defghiabc”. Solution There are 2 ways to…

## Interview Practice 25 – Longest Consecutive Digits

Question Given a function prototype: int continumax(char *output_string,char *input_string). Implement it to find the longest consecutive digits. This function must return the length of the longest digits. The found longest digits should be written to the memory location that output_string is pointing. For example, if input_string is “abcd12345ed125ss123456789”, function returns 9 and output_string becomes “123456789”. Solution This…

## Interview Practice 21 – Summing Combinations

Question Given a integers m and n, generate all combination within 1 to n that would give the sum m. For example, for m=5 and n=5, the combinations are {5}, {4+1}, {3+2}. Solution This is a knapsack problem. # knapsack that store the found combination knapsack = array # find combination of 1..n with sum target…

## Interview Practice 17 – Find The String Appeared Once

Question Given a string, write an algorithm to find the character inside that appeared only once. For example, the result for string “abaccdeff” is b. Solution We can use a hashtable to count the appearance times for each character. Then rescan the hashtable for the character that appeared only once. These two step take O(n)…

## Interview Practice 14 – Find Integer With Wanted Sum

Given a sorted integer list and an integer, find two integer in the list such that the sum of the two integers equals the given integer. It is required that the time complexity must be O(n).

## Interview Practice 13 – Last Kth Node of Linked List

Question Given a linked list, find the Kth last node in a linked list. The last 0th node is the tail node in the linked list. Solution Easy task. Construct 2 pointers: P1 and P2. First, both of them are set to head. Move P2 to the next Kth node. Example # node structure class…

## Interview Practice 12 – Sum from 1 to N Unconventionally

Question Sum up 1 to n without using division, multiplication, for loop, while loop, if else, switch and condition statement (A?B:C). Solution 1 Actually, for loop can be simulated through making use of the static property. Since static value will be stored in the class, we can increase the value every time we init a…

## Interview Practice 10 – Reverse Sentence

Simple task, reverse words in a sentence.

## Interview Practice 07 – Determine if Two Linked Lists Intersect

Question Given 2 linked list head pointers, determine whether they intersect at some point. Solution First of all, linked list can have loop or not, and this gives 3 possible situations. 1. For the 2 loops head pointer 1 and head pointer 2, move pointer 1 at normal speed, and pointer 2 at double speed….

## Interview Practice 06 – Appearance Count of Numbers

Question 腾讯面试题： 给你10分钟时间，根据上排给出十个数，在其下排填出对应的十个数 要求下排每个数都是先前上排那十个数在下排出现的次数。 上排的十个数如下： 【0，1，2，3，4，5，6，7，8，9】 Solution 初看此题，貌似很难，10分钟过去了，可能有的人，题目都还没看懂。 举一个例子， 数值: 0,1,2,3,4,5,6,7,8,9 分配: 6,2,1,0,0,0,1,0,0,0 0在下排出现了6次，1在下排出现了2次， 2在下排出现了1次，3在下排出现了0次…. 以此类推.. Sample Code [expand title=”Sample Code in C++” tag=”h4″] //数值: 0,1,2,3,4,5,6,7,8,9 //分配: 6,2,1,0,0,0,1,0,0,0 #include <iostream> using namespace std; #define len 10 class NumberTB { private: int top[len]; int bottom[len]; bool success; public: NumberTB(); int* getBottom(); void setNextBottom(); int getFrequecy(int num);…