PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles
All Puzzles Solved Unsolved

Find nth element from end of linked list

November 4, 2012 by asinghal Leave a Comment

How to find nth element from the end of the linked list?

Or in other words Given an integer ‘n’, return a pointer to the n-th node from the end of the list.
Note: Linked list is singly linked list (there is only next pointer no previous pointer).

See Solution: find nth element from end of linked listHide Solution

Keep two pointers pointer1 and pointer2, both pointing to head of the linked list initially. Increment pointer1 by one node till it reaches (n+1)th node. Now increment both pointers by one node till pointer1 becomes null. At this point the node which pointer2 is pointing to will be the nth node from the last.

Code in C/C++

Node * getNthNodeFromLast(Node * node, int n){
	Node* pointer1 = node;
	Node* pointer2 = node;

	while(pointer1 != null && n > 0){
		pointer1 = pointer1->next;
		n--;
	}
	//List does not have n nodes, so return null
	if(n > 0)
		return null;
	//traverse till pointer1 becomes null
	while(pointer1 != null){
		pointer1 = pointer1->next;
		pointer2 = pointer2->next;
	}
	return pointer2;
}

Checkout more Interview Questions Tags: Amazon Interview Questions, Easy, Linked List, Solved Puzzles

Find middle element of linked list

November 4, 2012 by asinghal 1 Comment

How to Find the middle element of a linked list without traversing twice ?

Linked list is singly linked list(there is only next pointer no previous pointer).

See Solution: Find middle element of a linked listHide Solution

Take two pointers both pointing to head of the linked list( pointer1 and pointer2 ). Now increment pointer1 by one node and pointer2 by two nodes at a time in a while loop till pointer 2 reaches end of the list or becomes null, at this point the node which pointer1 is pointing to will be the middle element/node of the linked list.

Code in C/C++

Node* getMiddleElement(Node * node){
	Node* pointer1 = node;
	Node* pointer2 = node;

	//traverse till pointer2 reaches end of the list or becomes null
	while(pointer2 != null && pointer2->next != null){
		pointer1 = pointer1->next;
		pointer2 = pointer2->next->next;
	}
	return pointer1;
}

Checkout more Interview Questions Tags: Easy, Interview, Solved Puzzles

Swap two integer variables

November 3, 2012 by asinghal Leave a Comment

How to swap two integer variables without using the third variable ?
See Solution: Swap two integer variablesHide Solution

I know three solutions, comment if you know more 🙂

Solution 1:

x = x+y;
y  = x-y;
x  = x-y;

Solution 2:

y = 2*((x+y)/2) – (x =y);

Solution 3:

x = x^y;
y = x^y;
x = x^y;

Note: However solution 1 & 2 has problem of overflow doing addition of two integers might overflow. But solution 3 will not have any such problem

Checkout more Interview Questions Tags: Easy, Interview, Solved Puzzles

Objective Questions set C/C++ – Part 5

October 28, 2012 by asinghal Leave a Comment

4 Objective type interview questions on C/C++ with answers

Before you start:-

  1. You can check your answer by filling a,b,c,d or e in the text box.
  2. You can get the answer by clicking Give Up Button.

Try yourself before giving up 🙂

Q1)

#include
int main(){
    char a[5]="hello";
    printf("%s",a);
}

Options: a) will print “hello” only b) will print some garbage values c) will print “hello” atleast d) None

Tried enough already?
[Read more…]

Checkout more Interview Questions Tags: C/C++, Interview, Solved Puzzles

Objective Questions set C/C++ – Part 4

October 28, 2012 by asinghal 5 Comments

10 Objective type interview questions with answers on C/C++

Before you start:-

  1. You can check your answer by filling a,b,c,d or e in the text box.
  2. You can get the answer by clicking Give Up Button.

Try yourself before giving up 🙂

Q1)

#include
main()
{
    unsigned char i;
    int sum=0;
    for(i=0;i<300;i++)
        sum=sum+i;
    printf("%d",sum);
}

Options: a) 44850 b) 45150 c) 300 d) Infinite Loop

Tried enough already?

Q2)

#include
fun(int i)
{
    int j=0;
    while (i & i-1) {
        j++;   
    }
    printf("%d",j);
}

main(){
    f(100);
}

Options: a. 100 b. 99 c. 2 d. 3

Tried enough already?

Q3)

Code 1 :
        for(i=0; i<1000; i++)
                for(j=0; j<100; j++)
                        x = y;

Code 2 :
        for(i=0; i<100; i++)
                for(j=0; j<1000; j++)
                        x = y;

Which code will execute faster ?
[Read more…]

Checkout more Interview Questions Tags: C/C++, Interview, Solved Puzzles

Objective Questions set C/C++ – Part 3

October 28, 2012 by asinghal 1 Comment

10 Objective type interview questions with answers on C/C++

Before you start:-

  1. You can check your answer by filling a,b,c,d or e in the text box.
  2. You can get the answer by clicking Give Up Button.

Try yourself before giving up 🙂

Q1) What will the below program prints ?

#include
void e( int );
main( )
{
    int a;
    a=3;
    e(a);
}

void e(int n)
{
    if (n>0)
    {
        e(--n);
        printf("%d",n);
        e(--n);
    }
}

Options: a) 0120 b) 0121 c) 0201 d) 0211

Tried enough already?
Q2)

#include
main( )
{
    int a[5]= {1,2,3,4,5};
    int*ptr = (int*)(&a+1);
    printf("%d %d", *(a+1), *(ptr-1));
}

Options: a)2 2 b) 2 1 c) 2 5 d) None

Tried enough already?
Q3)

#include
void foo(int [][3]);
main( )
{
    int a[3][3]={ {1,2,3}, {4,5,6}, {7,8,9} };
    foo(a);
    printf("%d", a[2][1]);
}

void foo(int b[][3])
{
    ++b;
    b[1][1]=35;
}

Options: a)8, b) 9, c) 7, d)35

Tried enough already?
Q4)
If performance is a critical requirement, then storage class should be used is ?
a)auto b) register c) extern d) static
Tried enough already?

Q5)
In a program
1-> char str1[ ] = “abc”;
2-> char *str2 = “xyz”;
3-> str2 = str1;
4-> str1 = str2;

Options:
a)compile error at line 3
b)compile error at line 4
c)no problem in the code
d)run time error

Tried enough already?

Q6)

int A = sizeof(char*)
int B = sizeof(int*)

a)A=B b)A<B c)A>B d) depends on compiler

Tried enough already?

Q7)

void func (int y)
{
static int x;
.
.
.
}

x and y will be stored on respectively in ?

Options:
a) stack and heap b) stack and data section c) data section and stack d) heap and data section

Tried enough already?

Q8)

#include

main(){
    int i=5;
    ++i++;
    printf("%d", i);
}

Options:
a)5 b) 6 c) 7 d) compilation error

Tried enough already?

Q9)
Statement 1-> const char *p1=”hello world”;
Statement 2-> char *const p2=”hello world”;
Statement 3-> p1=”welcome”;
Statement 4-> p2=”good bye”;
Statement 5-> *(p1+3) = ‘A’;
Statement 6­-> *(p2+3) = ‘A’;

Which statement cause problem ?

Options: a)4 & 5 b) 1 & 3 c) 3 & 5 d) 2 & 3

Tried enough already?

Q10)

#include
int function()
{
    static int a=0;
    a=a+1;
    return a;
}

main()
{
    function();
    function();
    printf("%d", function());
}

Options: a) 1 b) 2 c) 3 d) None

Tried enough already?

Checkout more Interview Questions Tags: C/C++, Interview, Solved Puzzles

Objective Questions set C/C++ – Part 2

October 28, 2012 by asinghal Leave a Comment

10 Objective type interview questions with answers on C/C++

Before you start:-

  1. You can check your answer by filling a,b,c,d or e in the text box.
  2. You can get the answer by clicking Give Up Button.

Try yourself before giving up 🙂

Q1)

#include
struct XX{
    int a:10;
    int b:6;
    char c;
} structure;
int main(){
    int i = sizeof(structure);
    printf("%d", i);
}

Options: a) 4 b) 8 c) 9 d) 3 e) Compilation error

Tried enough already?

Q2)

#include
#include
main()
{
    char *s;
    s="hot java";
    strcpy(s,"solaris java");
    printf("%s", s);
}

Options:a) hot java b) solaris java c) compilation error d) Segmentation fault

Tried enough already?
[Read more…]

Checkout more Interview Questions Tags: C/C++, Interview, Solved Puzzles

Objective Questions set C/C++ – Part 1

October 28, 2012 by asinghal Leave a Comment

10 Objective type interview questions with answers on C/C++

Before you start:-

  1. You can check your answer by filling a,b,c,d or e in the text box.
  2. You can get the answer by clicking Give Up Button.

Try yourself before giving up 🙂

Q1) What will be the output of the program below

#include
main(){
    int num[] = {1,4,8,12,16};
    int *p,*q;
    int i;
    p =  num;
    q = num+2;
    i = *p++;
    printf("%d, %d, %d\n",i, *p, *q);
}

Options a) 4, 4, 8 b) 1, 4, 8 c) 2, 1, 8 d) 2, 4, 8

Tried enough already?
Q2) What will be the output of the program below

#include

main(){
char *a[4]={"jaya","mahesh","chandra","swapant"};
int i = sizeof(a)/sizeof(char *);
printf("i = %d\n", i);
}

Options: a) 4 b) 7 c) 8 d) 1

Tried enough already?
[Read more…]

Checkout more Interview Questions Tags: C/C++, Interview, Solved Puzzles

  • « Previous Page
  • 1
  • …
  • 3
  • 4
  • 5
Submit your Puzzle

You may also like

  • Tic-Tac-Toe-Tomek Solution: Google codejam 2013 Qual Round
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2
  • Beautiful Strings solution: Facebook hacker Cup 2013 Qual Round
  • Detect if there is a cycle in a linked list
  • Output of the recursive program
  • Charging Chaos Solution – Google CodeJam
  • implement isPalindrome(int n)
  • Sort the array in O(N)
  • Objective Questions set C/C++ – Part 6
  • Find nth element from end of linked list

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▼
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous