Skip to main content

Coding Interview Questions Set 3


For previous  questions and answers please refer to  Coding Interview Questions Set 1 and
Coding Interview Set 2.
Let's begin with set 3,

7)How to reverse a singly linked list?

a)Simple recursive solution:

function reverseList(head) {
    if (!head || !head.next) {
        return head;
    }
    var newHead = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return newHead;
}

b)Iterative solution:

function reverseList(head) {
    var prev = null;
    while (head) {
        var next = head.next;
        head.next = prev;
        prev = head;
        head = next;
    }
    return prev;
}


8)Given a column title, return its corresponding column number. (Asked in Microsoft)
Write a function that accepts a string s.
Constraints:
1 <= s.length <= 7
s consists only of uppercase English letters.
s is between "A" and "FXSHRXW".

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28

Example 1:

Input: "A"
Output: 1


Example 2:
Input: "AB"
Output: 28

Example 3:
Input: "ZY"
Output: 701



Solution:
var titleToNumber = function(s) {
    const charCodeBase = 'A'.charCodeAt(0);
    const n = s.length;
    let result = 0;
    for (let i = 0; i < n; i++){
    result = result * 26 + (s.charCodeAt(i) - charCodeBase + 1);
    }
    return result ;
};
The charCodeAt() method returns the Unicode of the character
at the specified index in a string.
Logic:
Think of it as base 26.
For example,
Column number of "AB" = 1 * 26^1 + 2 * 26^0

I hope you like this article.
Please stay connected for coding interview questions set 4.

You can also follow me on Twitter or Linkedin for the latest updates.

Written By:

Saurabh Joshi

Comments

Popular posts from this blog

Node.js: Extract text from image using Tesseract.

In this article, we will see how to extract text from images using Tesseract . So let's start with this use-case, Suppose you have 300 screenshot images in your mobile which has an email attribute that you need for some reason like growing your network or for email marketing. To get an email from all these images manually into CSV or excel will take a lot of time. So now we will check how to automate this thing. First, you need to install Tesseract OCR( An optical character recognition engine ) pre-built binary package for a particular OS. I have tested it for Windows 10. For Windows 10, you can install  it from here. For other OS you make check  this link. So once you install Tesseract from windows setup, you also need to set path variable probably, 'C:\Program Files\Tesseract-OCR' to access it from any location. Then you need to install textract library from npm. To read the path of these 300 images we can select all images and can rename it to som...

Globant part 1

 1)call,apply,bind example? Ans: a. call Method: The call method is used to call a function with a given this value and arguments provided individually. Javascript code: function greet(name) {   console.log(`Hello, ${name}! I am ${this.role}.`); } const person = {   role: 'developer' }; greet.call(person, 'Alice'); // Output: Hello, Alice! I am developer. In this example, call invokes the greet function with person as the this value and passes 'Alice' as an argument. b. apply Method: The apply method is similar to call, but it accepts arguments as an array. Javascript code: function introduce(language1, language2) {   console.log(`I can code in ${language1} and ${language2}. I am ${this.name}.`); } const coder = {   name: 'Bob' }; introduce.apply(coder, ['JavaScript', 'Python']); // Output: I can code in JavaScript and Python. I am Bob. Here, apply is used to invoke introduce with coder as this and an array ['JavaScript', 'Pyt...

Maximizing MongoDB Performance: A Guide to Effective Indexing Strategies

Introduction: MongoDB, a leading NoSQL database, offers unparalleled flexibility and scalability. However, achieving optimal query performance in MongoDB often relies on implementing effective indexing strategies. In this article, we'll delve into various indexing techniques and provide real-world examples to demonstrate their impact on query performance. Single Field Index: Single field indexes are ideal for accelerating queries that filter, sort, or search based on a specific field. Let's consider a scenario where we have a collection of user profiles, and we frequently query users by their username field: db.users.createIndex({ "username": 1 }) This index significantly speeds up queries like: db.users.find({ "username": "john_doe" }) Compound Index: Compound indexes are invaluable when queries involve multiple fields. Suppose we have a collection of products and often filter by both category and price: db.products.createIndex({ "category...