HCL Interview

March 28, 2017 at 11:16 am | Posted in Patterns, Programming | Leave a comment

Earlier I wrote about my interview experience with NetCloud Systems Bangalore. Recently I appeared for an interview with HCL Technologies. (HCL Technologies is on the Forbes Global 2000 list.[13] It is among the top 20 largest publicly traded companies in India with a market capitalisation of $22.1 billion as of May 2015.[14] As of August 2015, the company, along with its subsidiaries, had a consolidated revenue of $6.0 billion.[5]  — Source: Wikipedia )

Interviewer was quite younger than me. He asked me to write a program using a singly linked list where he wants to remove Nth element counting from last node you added. e.g if you added 10 elements in the list then 7th element from end is 4th element you added in beginning, hence 4th should be removed. If 1,2,3,4,5,6,7,8,9,10 are the elements you added then “./a.out 7” should remove 4 not 7.

I told him I can use singly list as stack where elements are always added in reverse order. From his looks I could make out he could not understand what I just said. So I asked him if can I use any method,he replied that I should use pointers and gave  a paper and pen to write. This is the full-fledged code with all the checks and added command-line input and string conversion etc. but algorithm/logic/data is exactly same I wrote there and he said this code will not remove 7th from end but 5th from end.   This is wrong code. I was shocked to hear that. Loot at it yourself and see the output:

/* HCL Interview Question (2017)
 *
 * A singly-linked-list (SLL) program to add nodes to SLL and remove Nth node
 * from the end
 *
 * e.g Add 10 nodes to a SLL & remove the 7th node starting from the end
 * 7th node from end is 4th node you added while building SLL
 *
 * I am using a Stack (LIFO). Last node (the end) is always the latest. So, we can
 * go down from there easily. The interviewer said it will not work.
 * Worked fine for me 🙂
 *
 ***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include 				<limits.h>

struct node {
  long nn;
  struct node* next;
};

struct StPtr {
  struct node* head;
  long tn; /* totoal number of nodes */
};

struct StPtr* st;

void print_diagnostics(void);
void printStack(void);
void convert_to_long(long*, long*, const char*, const char*);
void addNodes(long, long);
void removeNode(long, long);

int main(int argc, char* argv[]) {
  if(3 != argc) {
    print_diagnostics();
    return EXIT_FAILURE;
  }

  st = malloc(sizeof*st);
  if(NULL == st) {
    printf("Out of Memory\n");
    return EXIT_FAILURE;
  }

  st->head = NULL;
st->tn = 0;
  long t = 0, r = 0;
  convert_to_long(&t, &r, argv[1], argv[2]);
  addNodes(t,r);
  printStack();
  removeNode(t, r);
  printStack();  

  return EXIT_SUCCESS;
}

void removeNode(long num, long rem) {
  if((0 >= num) || (0 >= rem) || (rem > num)){
    printf("Nothing to remove\n");
  }
  else {
    struct node* t = st->head;
    struct node* prev = st->head;
    long i = (t->nn - rem) + 1;
    /* IF we ar eremoving the head */
    if(i == st->head->nn) {
      st->head = st->head->next;
      st->tn--;
      printf("Removing node#: %ld \n", i);
      free(t);
    }
    else {
      while(i != t->nn) {
	prev = t;
	t = t->next;
      }
      prev->next = t->next;
      printf("Removing node#: %ld \n", i);
      st->tn--;
      free(t);
    }
  }
}

void addNodes(long num, long rem) {
  if((0 >= num) || (0 >= rem) || (rem > num)){
    printf("Nothing to add\n");
  }
  else {
    for(long i = 1; num >= i; ++i) {

      struct node* p = malloc(1 * (sizeof *p));
      if(NULL == p) {
	printf("Out of memory, will not add node\n");
      }
      else if(NULL == st->head) {
	printf("Adding 1st node\n");
	p->nn = i;
	p->next = NULL;
	st->head = p;
	st->tn++;
      }
      else {
	p->nn = i;
	p->next = st->head;
	st->head = p;
	st->tn++;
      }

    }
  }
}

void convert_to_long(long* num, long* rem, const char* numptr, const char* remptr) {
  errno = 0;  /* To distinguish success/failure after call */
  char* endptr;
  long temp = strtol(numptr, &endptr, 0);
  if( ((0 == temp) && (0 == strcmp(numptr, endptr)) && (errno == ERANGE))
      || ((LONG_MAX == temp) && (ERANGE == errno)) ) {
    *num = 0;
    *rem = 0;
  }
  else {
    *num = temp;
    temp = strtol(remptr, NULL, 0);
    if( ((0 == temp) || (LONG_MAX == temp)) && (ERANGE == errno) ) {
    *num = 0;
    *rem = 0;
    }
    else { *rem = temp; }
  }
}

void print_diagnostics(void) {
  printf("Invalid Number of Args\n");
  printf("Provide 2 arguments:\n");
  printf("\t1st arg is number of nodes\n\t2nd arg is nuber of node to be removed\n");
}

void printStack(){
  struct node* t = st->head;
  for(t = st->head; t; t = t->next) {
    printf("%ld, ", t->nn);
  }
  printf("\n\t---> Total %ld nodes\n", st->tn);
  printf("\n======================================\n\n\n");
}

OUTPUT:
[arnuld@arch64 programs]$ gcc -std=c99 -pedantic -Wall -Wextra ll.c
[arnuld@arch64 programs]$ ./a.out 10 7
Adding 1st node
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
—> Total 10 nodes

======================================

Removing node#: 4
10, 9, 8, 7, 6, 5, 3, 2, 1,
—> Total 9 nodes

======================================

[arnuld@arch64 programs]$ ./a.out 10 5
Adding 1st node
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
—> Total 10 nodes

======================================

Removing node#: 6
10, 9, 8, 7, 5, 4, 3, 2, 1,
—> Total 9 nodes

======================================

[arnuld@arch64 programs]$ ./a.out 10 1
Adding 1st node
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
—> Total 10 nodes

======================================

Removing node#: 10
9, 8, 7, 6, 5, 4, 3, 2, 1,
—> Total 9 nodes

======================================

[arnuld@arch64 programs]$ ./a.out 1 10
Nothing to add

—> Total 0 nodes

======================================

Nothing to remove

—> Total 0 nodes

======================================

[arnuld@arch64 programs]$

I don’t understand how could he say this code will not remove the Nth node from end , code behaves fine. May be he never wrote a Stack in his life. I did another mistake by trying to explain him that Stack is an implementation of a linked list and he shrugged it off. He asked me to write it using some other method (he meant another algorithm) but explain to him first and I did and he said same words again that this new method will not work either. Then I told him, there are several ways you can approach the problem that is why so many algorithms (methods 😉 ) exist and is there any specific method he is thinking of ?   He told me that he is done with me and I can leave for the day  🙂

After coming out I saw on the doors in capital letters, “ODC” was written. That means Offshore Development Center. Most of the ODCs in Indian companies are service based, there is not much development/coding involved. Companies in India, get freshers in a mass based campaigns from colleges and put them on work because they are dirt cheap to get when it comes to economy. These freshers have never worked before and most of engineering colleges here have non-ISO conforming compilers and code in their CS textbooks too does not conform to any ISO/ANSI standard. They have learned C and C++ using such resources, so it is expected that they wont know what is the definition of C language and how do ISO conforming compilers and non-forming compilers behave and what it costs in terms of bugs and maintenance.

Now lets get back to HCL, the company. What did they lose ?  I am just one programmer and what about others. There were 100 people for interview and I am sure at least 10 of them must be very good programmers. Will they get hired ?  I have been to interviews where I had to choose one answer out of 4 options given to me for   “int i = 0; prinf(“%d\n”, i++ + ++i)” and none of the options said “UB”. You were forced to choose an integer as an answer. What did I do ? I wrote “option5: UB” just below tho option 4 and of course I was not hired 🙂 . Can you imagine the quality of code in such a company, quality of hires ?   and what a nightmare it will be to fix bugs and add new features ?  HCL did not do much different from them in my interview. Whom to blame ?  Their hiring process which lets a person with low knowledge of Data-Structures interview a person with medium level of knowledge of Data-Structures. Who created such process. I know I am not that intelligent  because I have never written an AVL Tree and I can’t understand more than half of Maths Knuth writes in his Art of Computer Programming books. I just try to improve everyday and like to read definition of language, reference manuals and man pages and do what they advise. And I am still working on Knuth. In book Programming Interviews Exposed authors wrote that the more you stay away from coding and move towards business side, the more money you make and more secure your career will be (I did not know at that time that google’s machine learning department is working on making coding obsolete).  You make more money if you move from being programmer to business analyst and to software architect (A glassdoor search will tell you that it is really true). Now, how does a person who loves to program, who loves  to write code day and night, who loves to learn more languages, algorithms, more UNIX, Linux, DragonFlyBSD, NetBSD and more UNIX way of solving problems, data-structures, gcc libraries and learn more about programming paradigms,  loves Lisp and the idea behind its macros, loves to read why Eiffel was created and what problems it solves and why and how does it do it and wants to inculcate these as a part of his thinking, plus the shell he works in, and then mastering his text-editor etc. All that  just for the interest and liking. What that person can do to show his skill and get to work with MNC ? Not much if company is not even interested in having a skilled person and improving their software, tools and methodologies.  I think that challenge comes down to the programmer himself. Few companies want to improve, most don’t and you go and find those few companies.

Copyright © 2017 Arnuld Uttre, Hyderabd, Telangana – 500017 (INDIA)
Licensed Under Attribution-NoDerivs 3.0 United States (CC BY-ND 3.0 US)

Advertisements

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: