Skip to main content

Posts

Interview series [Java Script] : curious case of NaN

Have you ever tried to validate NaN using == or ===?  could you correctly guess the output of below statements? 1. x=parseInt("string") 2. console.log(x) 3. x==NaN 4. x===NaN 5. NaN===NaN 6. isNaN(x) well, if you correctly answerd all the statements then congtars, but if don't; don't worry, i will try to give a walk through. Let's first the output first - x=parseInt("string") NaN console.log(x) NaN x==NaN false x===NaN false NaN===NaN false isNaN(x) true It is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of an isNaN function. source - http://droidscript.org
Recent posts
You are given four integers 'a', 'b', 'y' and 'x', where 'x' can only be either zero or one. Your task is as follows: If 'x' is zero assign value 'a' to the variable 'y', if 'x' is one assign value 'b' to the variable 'y'. You are not allowed to use any conditional operator (including ternary operator).  Possible Solutions: 1. y = (1 - x) * a + x * b; 2.y = a ^ ((a ^ b) & (x))

Integer comparison using == gets tricky between -128 to 127.

Integer i=45;                                                                                                                                   Integer k=45;         System.out.println("i.hashcode= "+i.hashCode());         System.out.println("k.hashcode= "+k.hashCode());         System.out.println(i==k);         System.out.println(i.equals(k)); Out Put: i.hashcode= 45 k.hashcode= 45 true true Great, it seems work fine! now change the value of i=k=201.         Integer i=201;         Integer k=201;         System.out.println("i.hashcode= "+i.hashCode());         System.out.println("k.hashcode= "+k.hashCode());         System.out.println(i==k);         System.out.println(i.equals(k)); OutPut: i.hashcode= 201 k.hashcode= 201 false // System.out.println(i==k);   true  The reason behind this is the IntegerCache, a private static inner class in Integer class. This was feature was introduced in java 1.5 for performance improvemen

You have given an array in which numbers are first increasing and then decreasing. Find the maximum element in O(log n).

An integer array with unique elements has the following property – elements initially are in increasing order till a point after which they start to decrease. Implement a function to find the index of the maximum element in the array in less than linear time, i.e., O(n). Example: Input: array = {1, 3, 5, 7, 9, 8, 6, 4} Output: max index = 4 Max Index : 4.  Max Element value is : 9

Greedy algorithms for Job Scheduling

In this programming problem and the next you'll code up the greedy algorithms from lecture for minimizing the weighted sum of completion times.. This file describes a set of jobs with positive and integral weights and lengths. It has the format [number_of_jobs] [job_1_weight] [job_1_length] [job_2_weight] [job_2_length] ... For example, the third line of the file is "74 59", indicating that the second job has weight 74 and length 59. You should NOT assume that edge weights or lengths are distinct. Your task in this problem is to run the greedy algorithm that schedules jobs in decreasing order of the difference (weight - length). This algorithm is not always optimal. IMPORTANT: if two jobs have equal difference (weight - length), you should schedule the job with higher weight first. Beware: if you break ties in a different way, you are likely to get the wrong answer. You should report the sum of weighted completion times of the resulting schedule --- a posi

Interview Experience in a search based tech start up

once  i had an interview with a tech startup;  there were few programming problems; i am sharing one from that interview. Interview Location :Bangalore Company : A product based start up that works on search and big data. Round : 2 nd Level: semi difficult Program & Algo written on : white board Problem statement:   Given an array like A B C A A Z K I A Z Sort   the array based on frequency of character, if frequency is same sort them based on ASCII. {a=4,b=1,c=1,z=2,k=1,i=1} Out put : A A A A Z Z B C I K What i did  during that interview i am writing here: Algo To solve the problem:              1. Find their frequency  using hashtable.       2.  Prepare a class with {data : frequency } and put them in a list.       3. Sort  list using  Collections.sort(); to do that we should  implement comparable interface.       now prepare