Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
12
of 12 vote

Use Linked List concept. Let us say that the maximum limit of an integer in the language is 65,536. Let us limit it to 9999.
The counter will increase from 0-9999. After that, add another node, with value 1 and change the first node value to 0. Do this infinitely by adding nodes whenever needed.
Thus, we can easily print the counter value.

- Kishan January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u please explain??

- learner January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let us say in the language, the range of integers is from 0-65535.
Use only the nearest 10-powered value - 1. In this case, 10^4 - 1 = 9999.
Keep a counter and increment it until 9999 (say Node-1).
When this value is reached, on next increment, set this counter to 0. Add a node and set the counter to 1 (say Node-2).
Again, count from 0-9999 in Node-1 and on next increment, set Node-1 to 0 and Node-2 to 2.
Continue this way and add extra node whenever required.
Store all these nodes in a doubly linked list for easy access.

- emkishan January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, thanks for this logic can you please explain this part --->Again, count from 0-9999 in Node-1 and on next increment, set Node-1 to 0 and Node-2 to 2.

- Anand January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

For example,
Initially the linked list is
----------
| 1 |

It will be incremented until 9999
----------
| 9999 |
Now after the next increment, the linked list will add a new node as shown below:
---------- ----------
| 1| <---> | 0 |
This will happen until 19999. After this, again the 9999 is set to 0 and 1 will be added to other node's value. Thus, the linked list would be:
---------- --------
| 2 | <------->|0000|

This way, if we want to print the counter it is to print the values in the nodes together, which is 20000.
This way we can add nodes dynamically infinitely (until the system is out of memory ;) )

- emkishan January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

It's simple: just like counting in binary base, only that you use base 10000 :)

- not February 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. exactly !!

- emkishan February 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will it be possible to provide some code?

- Anonymous September 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A normal counter saved as a linked list when it hits max?

- bbarodia January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

l=[1,9,9,0,8]       
def inc():
    if l==[]:
        l.append(1)
    else:
        for i in range(len(l)-1,-1,-1):
            if l[i]==9:
                l[i]=0
                if l[i]==l[0]:
                    l.insert(0,1)
            else:
                l[i]+=1
                break
    return int(''.join(map(str,l)))

- Eugen January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sry, don't know python. Better give algo for your solution, instead of coding it.

- Nitin Gupta January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi ur code will noy work for like this i/p l=[0,9,9,0,9]

- gce.durai January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

use two variable, one is string and another is int64 M, we start with M = 0, use M till maximum value is hit, then we copy half of it to the string and start start again, if M becoming negative we bring max(M)/2 from string and add it to M, and start again,
for example Max(M) is 1000;
then we start from M = 0; when M is 1000 and one more add come, then we make M to 500 and add 500 to string, when M is 0 and one sub come then we make M to 500 and reduce 500 from string,
so by this way after at least 500 operation we perform one string add/sub. so on an avg we are just doing one add or one sub to integer per operation.

- sonesh January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello, I am having a hard time understanding what we accomplish by adding max(M)/2 to the string rather than max(M). Could you kindly explain the rational behind this.

- MiFry January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sonesh: Could you please elaborate your method by some code. Whatever you have written is too hard to understand.

Thanks.

- Dhonu121 January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def inc():
    l=[0,9,9,0,9]
    stack=[];
    if l==[]:
        l.append(1);
    else:
        carry=0;
        sum=l[len(l)-1]+1;
        if(sum<=9):
            l[len(l)-1]=sum;
            return int(''.join(map(str,l))); 
        else:
            carry=1;
            stack.append(0);
        for i in range(len(l)-2,-1,-1):
            sum=carry+l[i];
            if(sum>9):
                stack.append(0);
                carry=1;
            else:
                stack.append(sum);
                carry=0;
    stack.reverse();
    return int(''.join(map(str,stack)))

res=inc();
print "%s"%res;

- gce.durai January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This does not work for the special case like l=[9,9,9,9,9] (gives zero instead of 100000). Should have a if condition to automaticlly add a digit at front in this case.

- yuanfenw April 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int x = 0;
for( ; ;)
{
x++
}

Isnt this an infinite counter ?

- ashley January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, for most c based languages this will only be meaningful for 2^31 increments. After 2^31+1 increments we can no longer tell whether we overflowed once, twice or a trillion^2 times.

What the question asks is having a meaningful counter beyond this limitation and beyond unsigned long ints. So that's where the psuedo-infinite counter implementation comes in.

- MiFry January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Think like your gas station pump: i.e units increment until they reach nine than back to zero which triggers an increment in the tens, once the tens reach to nine it triggers an increment in the hundreds, etc. But this time our base won't be 10 it will be MAX_INT.

- Jasiri January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is similar to counting sticks
after every 4 1's ,we cut the 1111 with a diagnol line...so final value is 5.....we keep on doing this

- Anonymous February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public incrementedCount() {
        int carry = 1;
        Node currentNode = head;

        while(true) {
            if (currentNode == null) {
                currentNode = new Node();
                currentNode.data = carry;
                head = currentNode;
            }
            else {
                int num = currentNode.data;
                int sum = num+carry;

                currentNode.data = sum%10;
                carry = sum/10;

                if (currentNode.next == null && carry > 0) {
                    Node next = new Node();
                    next.data = carry;
                    currentNode.next = next;
                    break;
                }
            }
        }
    }

- srikantaggarwal February 26, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More