Interview Question


Country: India




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

/*
1->2->3->4->6->7->8->9   ,, K =3

1->2->3  4->5->6  7->8->9
789 456 123
-----

1->2->3->4->5->6->7->8->9   ,, K =2
1->2 3->4 5->6 7->8 9
9 78 56 3->4 1->2 
*/

struct listNode
{
	int data;
	listNode *next;
	listNode(int val) :data(val), next(NULL) {}
};

listNode * createLinkedList(int numberOfNodes)
{
	listNode *head = new listNode(1);
	int i = 2;
	listNode *p = head;
	while (i <= numberOfNodes)
	{
		p->next = new listNode(i);
		p = p->next;
		i++;
	}
	return head;
}

listNode * reverseBatch(listNode *head, int k)
{
	if (head == NULL || head->next == NULL || k < 2) //  below code does not work if k == 1
	{
		return head;
	}

	int i = 0;
	listNode  *prevHeader = NULL;
	listNode  *currentHeader = NULL;
	listNode *p = head;

	while (p != NULL)
	{
		if (i % k == k - 1)
		{
			listNode *temp = p;
			p = p->next;
			temp->next = prevHeader;
		}
		else
		{
			if(i % k == 0)
				prevHeader = currentHeader, currentHeader = p;
			p = p->next;
		}
		i++; 
	}

	head = currentHeader;;
	if (i % k != 0 ) 
	{
		p = head;
		while (p->next != NULL)
			p = p->next;
		p->next = prevHeader;
	}
	
	return head;
}

void printLinkedList(listNode *head)
{
	while (head != NULL)
		cout << head->data <<", ", head = head->next;
	cout << endl;
}

int main()
{
	listNode *head = createLinkedList(9);
	head = reverseBatch(head, 3);
	printLinkedList(head);
	// free list head
	head = createLinkedList(11);
	head = reverseBatch(head, 3);
	printLinkedList(head);
	// free list

	head = createLinkedList(11);
	head = reverseBatch(head, 2);
	printLinkedList(head);
	getchar();
	return 0;
}

- siva.sai.2020 November 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c# implementation.
key function.

private static void ReverseKBatchNodes<T>( ref Node<T> headNode, int k ) {

            if ( headNode == null ) { return; }
            Node<T> prevNodeBeforeGroup = GetPrevNodeBeforeGroup( headNode, k );
            if ( prevNodeBeforeGroup == null ) { return; }
            Node<T> currNode = prevNodeBeforeGroup.NextNode;
            int counter = -1;

            while ( currNode != null ) {

                counter++;

                if ( counter < k - 1 && currNode.NextNode != null) {
                    currNode = currNode.NextNode;
                    continue;
                }

                var tmpHead = prevNodeBeforeGroup.NextNode;

                prevNodeBeforeGroup.NextNode = currNode.NextNode;
                currNode.NextNode = headNode;

                headNode = tmpHead;

                currNode = prevNodeBeforeGroup.NextNode;
                counter = -1;
            }
        }

- zr.roman November 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

another part of code for testing the solution.

private static Node<T> GetPrevNodeBeforeGroup<T>( Node<T> headNode, int k ) {

            Node<T> prevNodeBeforeGroup = null;
            var currNode = headNode;

            for ( int i = 0; i < k - 1; i++ ) {
                prevNodeBeforeGroup = currNode.NextNode;
                currNode = currNode.NextNode;
                if ( currNode == null ) { return null; }
            }
            return prevNodeBeforeGroup;
        }

        private const int limit = 9;
        private const int K = 2;

        private static Node<int> CreateSinglyLinkedList( int n = 1 ) {

            var node = new Node<int> { Data = n };
            if ( n < limit ) {
                node.NextNode = CreateSinglyLinkedList( ++n );
            }
            return node;
        }

        public class Node<T> {
            public T Data { get; set; }
            public Node<T> NextNode { get; set; }
        }

        static void Main(string[] args) {
         
            Node<int> headNode = CreateSinglyLinkedList();
            ReverseKBatchNodes( ref headNode, K );

            var currNode = headNode;
            while ( currNode != null ) {
                 Console.Write( $"{currNode.Data} " );
                currNode = currNode.NextNode;
            }
            Console.ReadLine();
        }

- zr.roman November 30, 2015 | Flag


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